Hi,
I am relatively new to dos batch scripting and i need help with it.
I have a text file with 14 lines; Example:
"Time of last backup.....................#Wed May 01 00:45:03 EDT 2013"
"Attributes..................................#342985"
"Models......................................#1244"
a.s.o
Each line needs to be parsed analyzed and written into one or many variables. As output I need a single line, with ";" as deliminator for an excel input file. Example:
line1.day;line1.time;....;line2.NumOfAttributes;line3.NumOfModels;....
my issues is that string operations are working fine outside the loop, but not inside.
loop:
for /f "tokens=*" %%a in (%_parsein%) do (
set x1=%%a
echo.*** first line=%%a
echo.*** coppied line=%x1% /=> the variable x1 is not displayed
set "x1=%%x1:~0,10"
echo.%x1%
)
Outside the loop it works fine and i get the expected result.
set x1=tesSVFEWRGHERUt
echo.%x1%
set x1=%x1:~0,10%
echo.%x1%
any suggestions? help?
kp
trouble with for loop
Moderator: DosItHelp
Re: trouble with for loop
You are inside a code block. You need to enable delayed expansion and then use exclamation marks instead of percent symbols to reference your variables.
Re: trouble with for loop
Which loop ?haggis wrote:Hi,
I am relatively new to dos batch scripting and i need help with it.
I have a text file with 14 lines; Example:
"Time of last backup.....................#Wed May 01 00:45:03 EDT 2013"
"Attributes..................................#342985"
"Models......................................#1244"
a.s.o
Each line needs to be parsed analyzed and written into one or many variables. As output I need a single line, with ";" as deliminator for an excel input file. Example:
line1.day;line1.time;....;line2.NumOfAttributes;line3.NumOfModels;....
my issues is that string operations are working fine outside the loop, but not inside.
Where is parsein, what is parsein, a file a string ?haggis wrote:
loop:
for /f "tokens=*" %%a in (%_parsein%) do (
set x1=%%a
echo.*** first line=%%a
echo.*** coppied line=%x1% /=> the variable x1 is not displayed
set "x1=%%x1:~0,10"
echo.%x1%
)
Outside the loop it works fine and i get the expected result.
set x1=tesSVFEWRGHERUt
echo.%x1%
set x1=%x1:~0,10%
echo.%x1%
any suggestions? help?
kp
This is not a comment
/=> the variable x1 is not displayed
Percent expansion in a code block, is x1 externally defined ?
%x1%
You instructed for /f to use token a, yet acces token x ?
set "x1=%%x1:~0,10"
First line or all tokens ?
echo.*** first line=%%a
no idea what loop: means, stands for.
loop:
for /f "tokens=*" %%a in (%_parsein%) do (
set x1=%%a
echo.*** first line=%%a
echo.*** coppied line=%x1% /=> the variable x1 is not displayed
set "x1=%%x1:~0,10"
echo.%x1%
)
Code: Select all
@echo off &setlocal enableDelayedExpansion
>tst.TXT (
echo.a b c
echo.d e f
)
for /f usebackdelims^=^ eol^= %%? in (
"tst.TXT"
) do (
set "x1=%%?"
echo.*** first line=%%?
echo.*** coppied line=!x1! %= /=> the variable x1 is not displayed =%
set "x1=!x1:~0,10!"
echo.!x1!
)
pause
exit /b
Code: Select all
*** first line=a b c
*** coppied line=a b c
a b c
*** first line=d e f
*** coppied line=d e f
d e f
Druk op een toets om door te gaan. . .
Re: trouble with for loop
This is a simpler method - and uses delayed expansion.
I assumed that the surrounding quotes were added later.
I assumed that the surrounding quotes were added later.
Code: Select all
@echo off
set "_parsein=file.txt"
setlocal enabledelayedexpansion
for /f "tokens=1,* delims=#" %%a in (%_parsein%) do (
set "x=%%b"
set /p "=!x: =;!;"<nul
)
echo.
pause