0

This is extension of the code from Why the loop is not running as expected?

Ultimately I want to read values from Report.txt (a comma separated file having values like 1,2,3,4 in a single line) and update these values in some fields of summary.yml (...field10 : var1, field11 :var2,... field25 :var3..) etc. For that I am trying to store values from Report.txt but the variable are not being set. Any help would be highly appreciated

@echo off
setlocal enableextensions disabledelayedexpansion

rem E:\Backups\ code   \  Hazard \ test1 \ it0 ... itn
rem             ^root     ^ %%X    ^ %%Y           ^ %%~dpc

for /D %%X in ("*") do for /D %%Y in ("%%~fX\*") do for /f "tokens=1,2,*" %%a in ('
    robocopy "%%~fY." "%%~fY." Report.txt /l /nocopy /is /s /nc /ns /ts /ndl /njh /njs 
    ^| sort /r 2^>nul
    ^| cmd /q /v /c "(set /p .=&echo(!.!)"
') do (

    copy "%%~fY\it0\summary.yml" "%%~dpc."

    setlocal enabledelayedexpansion
    set vidx=0
    for /F "tokens=1-4 delims=," %%A in (%%~dpc\Report.txt) do (
        set "var1=%%A"
        set "var2=%%B"
        set "var3=%%C"
        set "var4=%%D"
    )
    set var



    @echo  !var1!>  %%~dpc\temp.txt
    @echo  !var2!>> %%~dpc\temp.txt
    @echo  !var3!>> %%~dpc\temp.txt
    @echo  !var4!>> %%~dpc\temp.txt
)
aschipfl
  • 33,626
  • 12
  • 54
  • 99
bob
  • 71
  • 1
  • 1
  • 6

1 Answers1

1

Your for /f, with a tokens=* is reading the full line without splitting it.

As the comma is a delimiter, you can split the line with a nested for loop

setlocal enabledelayedexpansion
for /F "tokens=*" %%A in (%%~dpc\Report.txt) do (
    for %%x in (%%A) do (
        SET /A "vidx=vidx + 1"
        set "var!vidx!=%%x"
    )
)
set var

Or, to directly split using the for /f you need to indicate the tokens in the line that you need to retrieve

for /F "tokens=1-4 delims=," %%A in (%%~dpc\Report.txt) do (
    set "var1=%%A"
    set "var2=%%B"
    set "var3=%%C"
    set "var4=%%D"
)
set var
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Thanks MC. In the console it shows that var1, 2,3,4 are getting set but when I am trying to read them these variables are blank – bob Oct 30 '15 at 19:00
  • 1
    @bob, Now your problem is not set the variable, but to read it. You need delayed expansion. Maybe [this](http://stackoverflow.com/a/30177832/2861476) could help you. – MC ND Oct 30 '15 at 19:03
  • this is very well explained. Thanks for giving your time to explain thoroughly. I tried it and in works but now it does run the loop just once. I am not sure why I am not able to paste my code here in the comments – bob Oct 30 '15 at 19:14
  • @ MC Editing my post to include the latest edits suggested by you. The problem now is that the code iterates just once – bob Oct 30 '15 at 19:16
  • @ MC added setlocal enableextensions disabledelayedexpansion after completing the loop block statements and before ). Looks it fixed my problem. Thanks a lot once again for your help. I'll moved to next step of the code and I'll need your help again :) – bob Oct 30 '15 at 19:29