Using variabes within vriables

May 25, 2011 at 06:57:49
Specs: Windows 7
Im trying to change a string varable (it will be used in a loop hopefully), to change a date from within a csv file...

The only way I can see of doing this using standard CMD is to use a for /f to take each line of my CSV file an make that line a string variable, then rename part of that string (the date).

My problem is that the date will change each day obviously, so I need to change the string to a new date each day.

I can create the date options to find and replace, making both of these variables (let calls them %date_old% and %date_new%), but when I try to use them to replace them in my string varialbe, they are ignored...

the line of code that I cant make work is... (this is what I've got, but its not working)

set bb=%aa:%old_date%=%newdate%%

Im assuming that there are just oo many %'s which is confuzing it..

I should point out that I'm also really not that great at CMD scripts... I very rarely have to use them...

Thanks all

See More: Using variabes within vriables

Report •

May 25, 2011 at 07:55:20
NOT tested

:: ==========================================
:: ricky.bat Wed 25-05-2011 21:46:51.03
@echo off & setLocal enableDELAYedeXpansioN

for /f "tokens=* delims= " %%a in (mycsv) do (
set S=%%a
set S=!S:%old_date%=%newdate%!

Life is too important to be taken seriously.


Report •

May 25, 2011 at 11:30:59
Thanks for that, but it didn't work... it just renames the variable to =!S:%old_date%=%newdate%!

I get...

C:\Users\Ricky\Desktop>for /f "tokens=* delims= " %a in (sage1201.csv) do (set S
=%a&set S=!S:%old_date%=%newdate%!)

C:\Users\Ricky\Desktop>(set S="si","hotel","NEW AC","0 ","250511","EOD,S
undry","T1",9.77 & set S=!S:250511=240511! )

If I echo %S% I would hope to get..
"si","hotel","NEW AC","0 ","240511","EOD,Sundry","T1",9.77

But what actually get is..

Maybe if I show you what I want... Here is an example of the .csv files I am creating (they happen to be for an import into Sage Line50, but that is irrelevant at this point)

"si","hotel","NEW AC","0 ","250511","EOD,Accommodation","T1",10.00
"si","hotel","NEW AC","0 ","250511","EOD,Food","T1",54.36
"si","hotel","NEW AC","0 ","250511","EOD,Drink","T1",26.82
"si","hotel","NEW AC","0 ","250511","EOD,Sundry","T1",9.77

The CSV contains dates for each line in the format of DDMMYY, which are all for the day the CSV is created (it is exported from another program, but the date cannot be changed at source)... I need to re-date each line of the csv to be for the day before the export. These files are generated daily though, so the batch file needs to be dynamic to alter the date correctly to the previous day, so I can't just use numbers, (I'm sure there is a better way of doing it than the way im working on, which uses a lot of IF statements to create the correct Y_DAY variable (yesterdays date) depending on the current T_DAY variable (todays date, formatted in DDMMYY)...

Can you suggest an easier way of changing the date in the CSV??

Thanks again...

Report •

May 25, 2011 at 12:02:26
:: ==========================================
:: ricky.bat Wed 25-05-2011 21:46:51.03
@echo off & setLocal enableDELAYedeXpansioN

set old_date=19
set newdate=22

for /f "tokens=* delims= " %%a in (mycsv) do (
set S=%%a
set S=!S:%old_date%=%newdate%!

Life is too important to be taken seriously.


Report •

Related Solutions

May 25, 2011 at 13:37:50
wow... I cant be totally sure until I get this back into work tomorrow and test it on some proper data... but I think with that bit its finally working... at least it seems to make the files now...

not that you're likely to ever want or need this, but my full script now is..

@echo off

::Set Path to be folder of Sage Files
SET PATH=C:\Users\Ricky\Desktop

::Set Variable for most recent csv
SET SAGE=Sage1201.csv
SET SAGE2=Sage1201b.csv

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%

if %dd%==08 (
set dd=8 ) else (
if %dd%==09 (
set dd=9 ) )
if %mm%==08 (
set mm=8 ) else (
if %mm%==09 (
set mm=9 ) )
set /A dd=%dd% - 1
set /A mm=%mm% + 0
if /I %dd% GTR 0 goto DONE
set /A mm=%mm% - 1
if /I %mm% GTR 0 goto ADJUSTDAY
set /A mm=12
set /A yyyy=%yyyy% - 1
if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
if %mm%==12 goto SET31
goto ERROR
set /A dd=31 + %dd%
goto DONE
set /A dd=30 + %dd%
goto DONE
set /A tt=%yyyy% %% 4
if not %tt%==0 goto SET28
set /A tt=%yyyy% %% 100
if not %tt%==0 goto SET29
set /A tt=%yyyy% %% 400
if %tt%==0 goto SET29
set /A dd=28 + %dd%
goto DONE
set /A dd=29 + %dd%
if /i %dd% LSS 10 set dd=0%dd%
if /I %mm% LSS 10 set mm=0%mm%

::Set Y_DAY variable to yesterday in ddmmyy format
set Y_DAY=%dd%%mm%%yyyy:~2%

::Run subscript on most recent csv to rename date and dump to
for /f "tokens=* delims= " %%a in (%PATH%\%SAGE%) do (
set S=%%a
set S=!S:%T_DAY%=%Y_DAY%!
echo.!S! >> %PATH%\%SAGE2%

and just for clarity...
the original csv file looked like this..

"si","hotel","NEW AC","0 ","250511","EOD,Accommodation","T1",10.00
"si","hotel","NEW AC","0 ","250511","EOD,Food","T1",54.36
"si","hotel","NEW AC","0 ","250511","EOD,Drink","T1",26.82
"si","hotel","NEW AC","0 ","250511","EOD,Sundry","T1",9.77"

and the new one..

"si","hotel","NEW AC","0 ","240511","EOD,Accommodation","T1",10.00
"si","hotel","NEW AC","0 ","240511","EOD,Food","T1",54.36
"si","hotel","NEW AC","0 ","240511","EOD,Drink","T1",26.82
"si","hotel","NEW AC","0 ","240511","EOD,Sundry","T1",9.77"

You are top draw ...

Cannot thank you enough

Report •

May 27, 2011 at 15:06:30
Just so keep things up-to-date... the script works grand...

Thanks again for the help

Report •

Ask Question