Solved

For loop not updating time variable

Posted on 2014-01-30
11
475 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 51

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now