Question

I've got a script that does everything I expect it to do, apart from one line.

I've done similar before, but I can't get this one to work.

The code I've got is here

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION 

::Set Path to be folder of Sage Files
SET PATH=C:\Welcome\Progs\SitesDataSetups\GeorgeYarmouth

::set date variables
for /f "tokens=1" %%i in ('date /t') do set thedate=%%i
set mm=%thedate:~3,2%
set dd=%thedate:~0,2%
set yyyy=%thedate:~6,4%

::Set T_DAY variable to date in ddmmyy format
set T_DAY=%dd%%mm%%yyyy:~2%

c:
cd\
cd %path%
for /f "usebackq tokens=* delims= " %%P in (`dir sage*.csv /od /b`) do (
    set SAGE=%%P
    set SAGE2=!SAGE:~0,8!_EDITED
    set EODNUM=!SAGE:~4,4!
    for /f "tokens=* delims= " %%A in (%%P) do (
        echo %EODNUM%
        set S=%%A

***This line is the problem***
        set S=!S:%T_DAY%=%EODNUM%!

        echo.!S! >> %PATH%\TEST\!SAGE2!.csv   
    )
)
endlocal

I was expecting that is would take each line of the csv file and replace it with itself, except with a string replace of the current date with the variable EODNUM (which it does... only the variable is expanded before it is set, so is nothing)... The delayed expansion should solve this, but I can use this line of code

set S=!S:%T_DAY%=!EODNUM!!

because I think its too many !'s for CMD.

Am I missing something, or is there a better way to code this?? (I'm not a programmer of any kind, and most of the code I write comes from trial and error, and 'borrowing' from other scripts, so this may be a very messy way to do this).

Was it helpful?

Solution

Transfer the the value of !EODNUM! to a FOR variable, and then use your FOR variable as the replace string.

        echo !EODNUM!
        set "S=%%A"
        for /f "delims=" %%E in ("!EODNUM!") do set "S=!S:%T_DAY%=%%E!"
        echo.!S!>> %PATH%\TEST\!SAGE2!.csv

OTHER TIPS

By way of explanation...

CMD reads (and does env var substitution), parses, and executes one top-level command at a time.

In your example, it reads the "for /f..." command all at once parsing and performing %var% substitution.

Once this is complete, it then executes the for loop, performing delayed !var! substitution.

Unfortunately, !var! substitution is not a do-substitution-until-none-left. This makes it hard (as in the answerer's solution) to perform the substitution into the !var:src=dst! value.

You will need a way that during execution you can get guaranteed substitution. This requires a for-statement, or something that involves reading and %var% substituting again. One way of doing this is to use the CALL :LABEL form where you can call to a specific label in your .cmd file and have this section do what you want:

...
call :GenS
...

and then:

:GenS
set S=!S:%T_DAY%=%EODNUM%!
goto :eof

BTW: I'm perplexed that you didn't notice the ECHO %EODNUM% not working in the loop as during the reading of the for loop all %var% substitutions are made.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top