|
Answer» Hello, I want to verify the content of the text read from the input file, before I transfer it to an out file. I did try to put its content into a variable, but it doesn't work. Some body can help me please? Thanks.
for /F "tokens=1* delims=]" %%j in ('type "%param%" ^| find /V /N ""') do ( Set subStr=%%k:~0,12%% )
However the following works: for /F "tokens=1* delims=]" %%j in ('type "%param%" ^| find /V /N ""') do ( echo.%%k>> "OutputFile" ) You have to set it as a variable or it will on work within a for loop. Thanks for the answer... But, if you want me to understand, could you WRITE down please. Thanks.SET var=%%k
Then when you want to use the info from %%k, use %var% instead. I did try it before, and one more time now, but %var% is set Null. So when I do SET var=%%k echo %var% --> I get "ECHO is off." (that's because the variable is set to NULL)
If you want to whole example, look what I've send this morning around 10:30
ThanksIn the FOR loop, make sure that TOKENS=*Newbie batch lesson #2 (That means you, helpmeh, who ought to know this by now!)
delayed expansion
When a batch file is run, normally, all variables are expanded ONCE ONLY, at parse time. This happens before the code is run.
In a FOR loop or other place where code is between parentheses, attempting to set a variable whose value is not known at parse time causes it to be set to a null value (a blank) so that attempting to echo the variable is interpreted by cmd.exe as just echo by itself.
Unless you use delayed expansion:
1. use the setlocal COMMAND with the option enabledelayedexpansion 2. within the parenthetical structure, use exclamation marks ! instead of PERCENT signs %
Code: [Select] @echo off
REM Do this once, e.g. at the start of the batch file setlocal enabledelayedexpansion
FOR /F "tokens=1* delims=]" %%j IN ('type "%param%" ^| find /V /N ""') DO (
REM get the value held in the FOR metavariable into a variable REM so we can slice it set subStr=%%k REM example: using echo to show value echo Value 1 of subStr=!subStr!
REM now we can cut it down to its FIRST 12 CHARS Set subStr=!subStr:~0,12! REM example: using echo to show value echo Value 2 of subStr=!subStr!
)
Incidentally, to protect against null variables producing "ECHO is off." messages at runtime, you can put some text, or at least a dot, after the echo, for example like this
echo.%var%
but if you get things right, you should not need to do this.
Thank you very much. It works great. The exclamation mark did all the difference.
|