Link to home
Start Free TrialLog in
Avatar of weklica
weklica

asked on

For loop not updating time variable

I have the following batch script.  however, it grabs the current time when it starts and writes over the file over and over instead of updating the file to the new name with the now current time.  How do I get the string to update for each loop (just milliseconds apart)

I replaced the % with ! to surreound the variables, but that didn't seem to work:

@echo ON
Setlocal EnableDelayedExpansion
For /f "tokens=2-4 delims=/ " %%a in ("%DATE%") do (
    SET YYYY=%%c
    SET MM=%%a
    SET DD=%%b
)
For /f "tokens=1-4 delims=/:." %%a in ("%TIME%") do (
    SET HH24=%%a
    SET MI=%%b
    SET SS=%%c
    SET FF=%%d
)
for /f "tokens=*" %%a in ('dir /a-d /b /s "i:\Images\"') do c:\dcmtk\bin\dcm2xml "%%a"
"z:\I_Images\%YYYY%%MM%%DD%%HH24%%MI%%SS%%FF%.xml"
for /f "tokens=*" %%a in ('dir /a-d /b /s "i:\V2\Images2\"') do c:\dcmtk\bin\dcm2xml "%%a"
"z:\I_ImagesV2\%YYYY%%MM%%DD%%HH24%%MI%%SS%%FF%.xml"
for /f "tokens=*" %%a in ('dir /a-d /b /s "i:\V3\Images3\"') do c:\dcmtk\bin\dcm2xml "%%a"
"z:\I_ImagesV3\%YYYY%%MM%%DD%%HH24%%MI%%SS%%FF%.xml"
for /f "tokens=*" %%a in ('dir /a-d /b /s "i:\V4\Images\4"') do c:\dcmtk\bin\dcm2xml "%%a"
"z:\I_ImagesV4\%YYYY%%MM%%DD%%HH24%%MI%%SS%%FF%.xml"
for /f "tokens=*" %%a in ('dir /a-d /b /s "P:\Images\"') do c:\dcmtk\bin\dcm2xml "%%a"
"z:\P_Images\%YYYY%%MM%%DD%%HH24%%MI%%SS%%FF%.xml"
Avatar of knightEknight
knightEknight
Flag of United States of America image

Dispense with the first two loops and try this:


for /f "tokens=*" %%a in ('dir /a-d /b /s "i:\Images\"') do c:\dcmtk\bin\dcm2xml "%%a"  "z:\I_Images\%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%%time:~9,2%.xml"

Open in new window

Well the problem is you are setting the time once at the beginning then using those variables in the rest of the code.  In general, if it is an option I suggest using my method from here to get dates but in this case you can probably get the date bit once then use the %time% variable removed of : and . in each iteration:

https://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html

e.g.

@echo off
Setlocal EnableDelayedExpansion
For /f "tokens=2-4 delims=/ " %%a in ("%DATE%") do SET D=%%c%%a%%b

call :ProcessImages "i:\Images\" "z:\I_Images"
call :ProcessImages "i:\V2\Images2\" "z:\I_ImagesV2"
call :ProcessImages "i:\V3\Images3\" "z:\I_ImagesV3"
call :ProcessImages "i:\V4\Images4\" "z:\I_ImagesV4"
call :ProcessImages "p:\Images\" "z:\P_Images"

exit /b

:ProcessImages

for /f "tokens=*" %%a in ('dir /a-d /b /s "%~1"') do (
  Set T=%time::=%
  Set T=!t:.=!
  c:\dcmtk\bin\dcm2xml "%%a" "%~2\!d!!t!.xml"
)
exit /b

Open in new window

Avatar of weklica
weklica

ASKER

This is what I did and it is the same result:

@echo ON
Setlocal EnableDelayedExpansion
for /f "tokens=*" %%a in ('dir /a-d /b /s "i:\Images\"') do c:\dcmtk\bin\dcm2xml "%%a" "z:\I_Images\%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%%time:~9,2%.xml"

Open in new window

Avatar of weklica

ASKER

dragon-it, that does the same thing,  Essentially, same time stamp each pass.

Actually, keeping the same name would also be fine as they are all definately unique.  That will suffice if it is easier...
in which case just use %%~dpfa.xml

thats the drive, path, and filename but not extension.

steve
Avatar of weklica

ASKER

I put it like this, but it is wrong.  I just want the filename with xml in the z:\ directories.  THis puts the xml version in itslef in the I:\source.  

@echo on
Setlocal EnableDelayedExpansion
For /f "tokens=2-4 delims=/ " %%a in ("%DATE%") do SET D=%%c%%a%%b

call :ProcessImages "i:\Images\" "z:\I_Images"
call :ProcessImages "i:\V2\Images2\" "z:\I_ImagesV2"
call :ProcessImages "i:\V3\Images3\" "z:\I_ImagesV3"
call :ProcessImages "i:\V4\Images4\" "z:\I_ImagesV4"
call :ProcessImages "p:\Images\" "z:\P_Images"

exit /b

:ProcessImages

for /f "tokens=*" %%a in ('dir /a-d /b /s "%~1"') do (
  Set T=%time::=%
  Set T=!t:.=!
  c:\dcmtk\bin\dcm2xml "%%a" "%%~dpfa.xml"
)
exit /b

Open in new window

Avatar of weklica

ASKER

So, this is the simplest version it seems to me.  But, how do I get just the filename into the destination directory?  Putting %%a at end of z:\I_Images obviously doesn't work.  I dont' want the whole I:\... path after the z:\path.  

for /f "tokens=*" %%a in ('dir /a-d /b /s "i:\Images\*.*"') do c:\dcmtk\bin\dcm2xml "%%a" "z:\I_Images\"

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of weklica

ASKER

This one was it.  Perfect.  Thanks!
Had to get some kip there, was 1am.... upt you but look at the method using call I suggested too when you have to repeat nearly the same code multiple times...
I mean replacing the for part with what you are happy with now ...