1.

Solve : Problems with FOR /F loop in XP.?

Answer»

The following batch file creates a text file and parses lines from it
BUT inside the For loop, SET doesnt store the parsed values into environment variables.

I have TRIED everything I can think of. 
All suggestions very welcome.
Thanks.

The aim is to write each line of the source file into a new text file by editing strings 2 and 4 in the source file to replace occurences of 'E:' in the source by 'D:' in the new file.

echo off
goto :start
Problems with FOR /F loop.
:start
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
   Rem first create a source file temp1.txt using * delimiters.
 Echo inG10*c:*\"My Documents"*e:*\"My Music">c:\temp1.txt
 Echo inG10*e:*\"My Documents"*d:*\"My Music">>c:\temp1.txt
   Rem and an empty output file temp2.txt 
 Echo Rem output file>c:\temp2.txt

  Rem now parse the Source file and work with strings 2 and 4.
FOR /F "tokens=1,2,3,4,5* delims=*" %%A in (C:\temp1.txt) do (
  Echo Parsed values:-  ppA=%%A; ppB=%%B; ppC=%%C; ppD=%%D; ppE=%%E
       set ppA=%%A
       Set ppB=%%B
       Set ppC=%%C
       Set ppD=%%D
       Set ppE=%%E
  Echo ppA=%ppA%; ppB=%ppB%; ppC=%ppC%; ppD=%ppD%; ppE=%ppE%
Echo In my computer, none of the above variables GET Set.

Echo Nor can I get a result using IF statements.
   If /I "%%B"=="e:" Set  ppB=D:
   If /I "%%D"=="E:" Set  ppD=D:

Echo Now write the output file with edited strings 2 and 4
    Echo %%A*%ppB%*%%C*%ppD%*%%E>>c:\temp2.txt

Echo In my computer this has empty 2nd and 4th strings.
pause
    )
)
Type c:\temp2.txtCosmic,

REM I believe you had the values assigned to ppx  variables.  But to extract the values while in the for loop, we use !ppA! I'm not sure how you were creating the temp1.txt file so I used a test file.  Hope this helps.  You still need work.  Good Luck.

p.s. I only SEE a need for: SETLOCAL ENABLEDELAYEDEXPANSION  one time

C:\>type cosmic.bat
Code: [Select]echo off
SETLOCAL ENABLEDELAYEDEXPANSION

 Echo a: b: c: d: e: > c:\temp1.txt
 Echo a: e: c: E: e: >> c:\temp1.txt

FOR /F "tokens=1,2,3,4,5* delims= " %%A in (C:\temp1.txt) do (

       set ppA=%%A
       Set ppB=%%B
       Set ppC=%%C
       Set ppD=%%D
       Set ppE=%%E
echo ppA=!ppA! ppB=!ppB! ppC=!ppC! ppD=!ppD! ppE=!ppE!


   If !ppB!=="e:" Set  ppB=D:
   If !ppD!=="E:" Set  ppD=D:
echo ppA=!ppA! ppB=!ppB! ppC=!ppC! ppD=!ppD! ppE=!ppE!

    Echo !ppB! !ppD!> c:\temp2.txt

)echo ppA=%ppA% ppB=%ppB% ppC=%ppC% ppD=%ppD% ppE=%ppE%

Type c:\temp2.txt

Output:

C:\>cosmic.bat
ppA=a: ppB=b: ppC=c: ppD=d: ppE=e:
ppA=a: ppB=b: ppC=c: ppD=d: ppE=e:
ppA=a: ppB=e: ppC=c: ppD=E: ppE=e:
ppA=a: ppB=e: ppC=c: ppD=E: ppE=e:
ppA=a: ppB=e: ppC=c: ppD=E: ppE=e:
e: E:
C:\>Me again, panic over, problem solved.
 
I copied the FOR /F approach from another post in this forum and GOT it working.
The trick seemed to be use Set in the body of the FOR statement,  then put the Echo and IF statements in a SUBROUTINE.  Did not need to use !variable!.  It all worked fine using %variable%.
Thanks for letting me share my problem and learn from your various threads.
G.



Discussion

No Comment Found