How Use special | > < = & in variable Dos

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

How Use special | > < = & in variable Dos

#1 Post by darioit » 24 Nov 2010 04:55

Hello,

I have this file

abcdefghilmn1| pqrstuvwxyz1
abcdefghilmn2& pqrstuvwxyz2
abcdefghilmn3= pqrstuvwxyz3
abcdefghilmn4< pqrstuvwxyz4
abcdefghilmn5> pqrstuvwxyz5

I using this batch for read/write each line

Code: Select all

@echo off
for /f %%a in (file_inp.txt) do (
  echo %%a >> file_out.txt
)


Why the result is truncked?
abcdefghilmn1|
abcdefghilmn2&
abcdefghilmn3=
abcdefghilmn4<
abcdefghilmn5>

How can I solve this issue?

Thanks
Dario

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: How Use special | > < = & in variable Dos

#2 Post by amel27 » 24 Nov 2010 05:28

Code: Select all

@echo off
for /f "delims=" %%a in (file_inp.txt) do (
  >>file_out.txt echo %%a
)

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: How Use special | > < = & in variable Dos

#3 Post by darioit » 24 Nov 2010 07:29

Thanks it works, but I need to run the batch in this way, and it doesn't works :(


Code: Select all

@echo off
FOR /F "delims=" %%a in (file_inp.txt) do set "name=%%a" &call :procName
goto:eof

:procName
>> file_out.txt echo %name%


Regards
Dario

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

Re: How Use special | > < = & in variable Dos

#4 Post by jeb » 24 Nov 2010 11:07

Hi darioit,

This should work.

Code: Select all

@echo off
setlocal DisableDelayedExpansion
FOR /F "delims=" %%a in (file_inp.txt) do set "name=%%a" &call :procName
goto:eof

:procName
setlocal EnableDelayedExpansion
>> file_out.txt echo(!name!
endlocal
goto :eof


Your problem was that after the percent-expansion the special characters like & are executed and therefore the line >> file_out.txt echo %name% fails.
With the delayed expansion it isn't a problem, because the & are not interpreted after the delayed expansion.

hope it helps
jeb

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: How Use special | > < = & in variable Dos

#5 Post by orange_batch » 24 Nov 2010 16:40

To find a solution without using enabledelayedexpansion, writing straight into Command Prompt I discovered:

Code: Select all

set var="asdf& asdf"
echo:%var:"=%
Does not work.



However, this does get past the command character interpretation phase:

Code: Select all

set "var=asdf& asdf"
for %x in ("%var%") do @echo:%~x

(variation)

Code: Select all

set var="asdf& asdf"
for %x in (%var%) do @echo:%~x


Also this:
set "var=asdf& asdf"
for /f "delims=" %x in ("%var%") do @echo:%x

(variation)
set var="asdf& asdf"
for /f "delims=" %x in (%var%) do @echo:%x

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

Re: How Use special | > < = & in variable Dos

#6 Post by jeb » 24 Nov 2010 18:08

Ok, there is a solution without delayed expansion, but it's much more complicated.

And a real solution should solve all variants of your tests with only one code.
I suppose a test case could be something like

Code: Select all

@echo off
setlocal DisableDelayedExpansion
FOR /F "delims=" %%a in (file_inp.txt) do (
    set "name=%%a"
    *** MAKE IT BULLET PROOF without delayed expansion ***
    echo %name%
    call :func %name%
)
goto :eof

:func
echo %1
goto :eof


And the input file should contain all critical strings.
Btw. I don't believe that there exists a solution, to use the same %name% with echo and also with call.

jeb

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: How Use special | > < = & in variable Dos

#7 Post by amel27 » 25 Nov 2010 00:05

another problem - replacing substring with "=" char w/o char by char reading,
for example, remove substring ",var2=2" in string "var1=1,var2=2,var3=3"

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

Re: How Use special | > < = & in variable Dos

#8 Post by jeb » 25 Nov 2010 06:01

Ok replacing the "=" and ":" are also interesting issues (in my humble opinion).

Therefore, I open a new thread for it.

jeb

Post Reply