Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 492
  • Last Modified:

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"
0
weklica
Asked:
weklica
1 Solution
 
knightEknightCommented:
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

0
 
Steve KnightIT ConsultancyCommented:
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:

http://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

0
 
weklicaAuthor Commented:
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

0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
weklicaAuthor Commented:
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...
0
 
Steve KnightIT ConsultancyCommented:
in which case just use %%~dpfa.xml

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

steve
0
 
weklicaAuthor Commented:
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

0
 
weklicaAuthor Commented:
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

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

Open in new window

Or if you want to force an XML file on the destination do:
for /f "tokens=*" %%a in ('dir /a-d /b /s "i:\Images\*.*"') do c:\dcmtk\bin\dcm2xml "%%a" "z:\I_Images\%%~na.xml"

Open in new window

~bp
0
 
weklicaAuthor Commented:
This one was it.  Perfect.  Thanks!
0
 
Steve KnightIT ConsultancyCommented:
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...
0
 
Steve KnightIT ConsultancyCommented:
I mean replacing the for part with what you are happy with now ...
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now