Solved

For loop not updating time variable

Posted on 2014-01-30
11
490 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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 56

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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 …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

630 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