Page 1 of 1

How Use special | > < = & in variable Dos

Posted: 24 Nov 2010 04:55
by darioit
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

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

Posted: 24 Nov 2010 05:28
by amel27

Code: Select all

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

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

Posted: 24 Nov 2010 07:29
by darioit
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

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

Posted: 24 Nov 2010 11:07
by jeb
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

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

Posted: 24 Nov 2010 16:40
by orange_batch
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

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

Posted: 24 Nov 2010 18:08
by jeb
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

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

Posted: 25 Nov 2010 00:05
by amel27
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"

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

Posted: 25 Nov 2010 06:01
by jeb
Ok replacing the "=" and ":" are also interesting issues (in my humble opinion).

Therefore, I open a new thread for it.

jeb