Solved

For loop not updating time variable

Posted on 2014-01-30
11
480 Views
Last Modified: 2014-01-30
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
Comment
Question by:weklica
11 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 39822784
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39822797
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
 

Author Comment

by:weklica
ID: 39822836
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:weklica
ID: 39822903
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39822936
in which case just use %%~dpfa.xml

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

steve
0
 

Author Comment

by:weklica
ID: 39822969
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
 

Author Comment

by:weklica
ID: 39823007
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
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39823038
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
 

Author Closing Comment

by:weklica
ID: 39823187
This one was it.  Perfect.  Thanks!
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39823283
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39823286
I mean replacing the for part with what you are happy with now ...
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question