Q. How can I keep copies of the same file names that I download multiple times in a day?

Jerold Schulman

February 7, 2001

2 Min Read
ITPro Today logo in a gray background | ITPro Today

I FTP download the same set of file names multiple times in a day,
and I was using a date/time folder name to store them. This was becoming unmanageable.
I have developed a new solution, which I am presenting it here, more as a demonstration of technique,
than as an actual solution that you might employ.

I decided to store all the files in one folder, using a FileName_YYYYMMDD_HHMMSShs.Extension format.
This preserves the original file name and extension, guarantees uniqueness,
and documents when the files were received. To accomplish the task, Using techniques from:

tip 0494 How to parse a batch parameter?
tip 1986 How can I get an accurate time in a batch job?
tip 3243 Windows 2000 FOR command enhancements.

I added the following to the end of the download batch:

FOR /F "usebackq tokens=*" %%n IN (`dir /b /a-d`) DO @FOR /F "usebackq tokens=2,3,4 delims=/ " %%d IN (`date /t`) DO @FOR /F "usebackq tokens=5-8 delims=:. " %%t IN (`echo.^|time`) DO @copy "%1%%n" "%%~nn_%%f%%d%%e_%%t%%u%%v%%w%%~xn"

Analysing the command string

The first FOR command parses the dir %1 /b /a-d, capturing the file name(s) in the %%n parameter.Embedded spaces are allowed because of the * token.The second FOR parses the Date /t command. In my configuration, date /t returns day mm/dd/yyyy. The "tokens=2,3,4 delims=/ " returns mm, dd, and yyyy as %%d, %%e, and %%f, respectively.If you type echo.|time at a command prompt, it returns:The current time is: HH:MM:SS.hsEnter the new time:The fourth FOR use "tokens=5-8 delims=:. " to parse HH, MM, SS, and hs as %%t, %%u, %%v, and %%w. Using the COPY command, it composes the source file path as "%%n" in the current folder. The destination file  is composed using , the file name component of %%n (%%~n), _, yyyymmdd (%%f%%d%%e), _, HHMMSShs (%%t%%u%%v%%w), and the original file extension (%%~xn).

I have created CopyNames.bat, for possible inclusion in other processes. The syntax for using CopyNames.bat is:

CopyNames

CopyNames.bat contains:

@echo offsetlocalif {%1}
{} goto syntaxif {%2}
{} goto syntaxif NOT exist %1/*.* goto syntaxif NOT exist %2 goto syntaxFOR /F "usebackq tokens=*" %%n IN (`dir %1 /b /a-d`) DO @FOR /F "usebackq tokens=2,3,4 delims=/ " %%d IN (`date /t`) DO @FOR /F "usebackq tokens=5-8 delims=:. " %%t IN (`echo.^|time`) DO @copy "%1%%n" "%2%%~nn_%%f%%d%%e_%%t%%u%%v%%w%%~xn"goto end:syntax @echo ****************************@echo CopyNames {From Folder Path} {To Folder Path} @echo ****************************:endendlocalNOTE:If the source folder contained a file named Products.MDB,     the destination folder might contain Products_20010205_12511475.MDB.


Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like