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

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" "<Destination Folder>\%%~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.hs
Enter 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 <Destination Folder>\, 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 <From Folder Path> <To Folder Path>

CopyNames.bat contains:

@echo off
if \{%1\}

\{\} goto syntax if \{%2\}

\{\} goto syntax if NOT exist %1/*.* goto syntax if NOT exist %2 goto syntax FOR /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 **************************** :end endlocal NOTE:If the source folder contained a file named Products.MDB, the destination folder might contain Products_20010205_12511475.MDB.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.