trouble with for loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
haggis
Posts: 1
Joined: 04 Mar 2014 14:31

trouble with for loop

#1 Post by haggis » 04 Mar 2014 14:51

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

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: trouble with for loop

#2 Post by Squashman » 04 Mar 2014 15:10

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.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: trouble with for loop

#3 Post by Ed Dyreen » 04 Mar 2014 15:25

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.
Which loop ?
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
Where is parsein, what is parsein, a file a string ?

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. . .

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: trouble with for loop

#4 Post by foxidrive » 04 Mar 2014 17:12

This is a simpler method - and uses delayed expansion.
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

Post Reply