How to preserve an exclamation mark in User's Free Text

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

How to preserve an exclamation mark in User's Free Text

#1 Post by alan_b » 15 Feb 2012 17:14

I have to handle a file with 6000 lines of text, and can do so in 2 Seconds.
I can accept some "reasonable" increase in time taken to get the correct results

This is my code, it works on 5970 lines out of 6000, but 30 lines happen to have exclamation marks that get lost.

Code: Select all

<%1 (
  for /L %%j in (!LO!,1,!HI!) do (
    SET "LN=" & SET /P "LN="
    FOR /F "tokens=1* delims=]" %%a in ("!LN!") DO ECHO(%%b
  )
) > %2

The intention is to accept file %1 with text such as
100265 101950 ]This user text is exciting ! This is Good.
and write to file %2 the original user text
This user text is exciting ! This is Good.

But the output is regrettably :-
This user text is exciting This is Good.

Is there any easy way to preserve any user text ! exclamations ?

I have control of the prefix such as
100265 101950 ]
This is always fixed width. It was needed for special sorting sequences.

After a good night's sleep I will try something like

Code: Select all

<%1 (
  for /L %%j in (!LO!,1,!HI!) do (
    SET "LN=" & SET /P "LN="
    ECHO(!LN:~16!
  )
) > %2

If that works then there could be a future maintenance problem if the sequence sorting should need 7 digit numbers,
in which case !LN:~16! might need changing to !LN:~18!
Sorting Prefix code is a few hundred lines away from this Prefix Stripping code,
so keeping two chunks of code "synchronized" could be a challenge :cry:

Regards
Alan

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to preserve an exclamation mark in User's Free Text

#2 Post by foxidrive » 15 Feb 2012 19:23

Toggle delayed expansion - it works here.

Code: Select all

@echo off
set "LN=100265 101950 ]This user text is exciting ! This is Good."
setlocal enabledelayedExpansion

for /f "delims=" %%z in ('dir /b /ad') do (
FOR /F "tokens=1* delims=]" %%a in ("!LN!") DO (
      setlocal disabledelayedexpansion
      ECHO(%%b
      setlocal enabledelayedexpansion
)
)

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: How to preserve an exclamation mark in User's Free Text

#3 Post by Liviu » 15 Feb 2012 19:34

alan_b wrote:If that works then there could be a future maintenance problem if the sequence sorting should need 7 digit numbers, in which case !LN:~16! might need changing to !LN:~18!

Maybe !LN:*]=! then?

Liviu

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to preserve an exclamation mark in User's Free Text

#4 Post by Ed Dyreen » 15 Feb 2012 23:18

'

Code: Select all

@echo off &setlocal disableDelayedExpansion

>testIN.TMP echo.Hello]World !

<testIN.TMP (

   for /l %%! in (1,1,1) do set $=&set/p$=&&(

      setlocal enableDelayedExpansion
      set $=!$:^"=""!%=                              pre-delay    =%
      set "$=!$:^=^^!"&call set "$=%%^$:^!=#"#"^!%%"!%=    delay 1-times    =%
      set $=!$:#"#"=^^^!&set $=!$:""=^"!%=          post-delay    =%
      for /f "tokens=1* delims=]" %%a in ("!$!") do (
         set "$=%%b"
         echo(a=%%a_,b=%%b_,$=!$:~0,2!_
      )
      endlocal
   )
)>testOU.TMP

type testOU.TMP
pause
exit

Code: Select all

a=Hello_,b=World !_,$=Wo_
Druk op een toets om door te gaan. . .

jeb
Expert
Posts: 1062
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: How to preserve an exclamation mark in User's Free Text

#5 Post by jeb » 16 Feb 2012 01:19

@foxidrive

foxidrive wrote:Toggle delayed expansion - it works here.

Code:
@echo off
set "LN=100265 101950 ]This user text is exciting ! This is Good."
setlocal enabledelayedExpansion

for /f "delims=" %%z in ('dir /b /ad') do (
FOR /F "tokens=1* delims=]" %%a in ("!LN!") DO (
setlocal disabledelayedexpansion
ECHO(%%b
setlocal enabledelayedexpansion
)
)


This way it fails with too much lines, as it isn't toggling, it's creating permanently new setlocal-scopes.

Toggling should look like

Code: Select all

@echo off
setlocal DisableDelayedExpansion
(
   for /F "delims=" %%a in (file1.txt) do (
      set "line=%%a"
      setlocal EnableDelayedExpansion   
      echo(!line!
      endlocal
   )
)


The "set/p"-way to read the lines is itself safe against exclamation marks, but lose all control characters at the line end (TAB and so on)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to preserve an exclamation mark in User's Free Text

#6 Post by foxidrive » 16 Feb 2012 01:53

jeb wrote:@foxidrive
This way it fails with too much lines, as it isn't toggling, it's creating permanently new setlocal-scopes.

Toggling should look like
[snip]
The "set/p"-way to read the lines is itself safe against exclamation marks, but lose all control characters at the line end (TAB and so on)




I see what you mean Jeb - though the OP seems to need delayed expansion for the initial part of the script so it would have to be toggled off before the echo in my example with an endlocal and that would clear any variables that were set, before being toggled on again with a setlocal enabledelayedexpansion. Not useful here I think.

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: How to preserve an exclamation mark in User's Free Text

#7 Post by alan_b » 16 Feb 2012 03:02

Many thanks for the prompt suggestions.

I like the idea of
!LN:*]=!
I had not realized that was possible.

Failing that I will try toggling DelayedExpansion.

Regards
Alan

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: How to preserve an exclamation mark in User's Free Text

#8 Post by alan_b » 16 Feb 2012 06:10

Liviu wrote:Maybe !LN:*]=! then?
Liviu

Many thanks it works perfectly.
And even better, this code now takes only 1880 mSec instead of 1960 mSec to process all 6000 lines of text.

Regards
Alan

Post Reply