Friday 15 April 2011

Trouble in Batch using quotes in a variable -



Trouble in Batch using quotes in a variable -

relatively new batch scripts here , have been searching everywhere reply not find anything.

here have batch script far..

@echo off set addtext="text add together includes spaces" /f "delims=" %%l in (file.txt) ( echo %%l %addtext% >> tmpfile.txt )

i'm looking add together line of text every line in file problem comes in double quotes. don't want quotes display text. have quotes there because there spaces in string of text i'm looking add together every line.

@echo off setlocal enableextensions disabledelayedexpansion set "addtext=text add together includes spaces" /f "delims=" %%l in (file.txt) ( >> tmpfile.txt echo %%l %addtext% )

this should work. not include quotes in value of variable, utilize them wrap assignment.

in cases string contain more problematic characters, safer version

@echo off setlocal enableextensions disabledelayedexpansion set "addtext=text add together includes spaces, > redirections & more problems !" (for %%a in ("%addtext%") /f "delims=" %%l in (file.txt) ( echo %%l %%~a )) >> tmpfile.txt quotes not included in value, wraps assignation to prevent problem accessing variable, wrapped in quotes, stored in for replaceable parameter (%%a) , when requested echoed without quotes (%%~a) just improve performance (should used in first code) instead of open/write/close output file each line (redirection each echo), redirection handled total for command.

batch-file quotes

No comments:

Post a Comment