Need multiple files moved and renamed with date-time stamp

Hello Experts.

We have a file that we had been importing every five minutes. The script we had been using assumed one file and it was moved and renamed when the script was run. Unfortunately our file provider is now sending multiple files and the script does not take that into account.

The file names will always start with the first 5 letters the same and then have a few characters that the provider adds to the file name and then ending with a .TXT file type.

IE Vexportbackup093020130830.txt
     Vexportbackup093020130831.txt

Below is the script that had been in place that handled one file at a time.
=================================
cls
echo off

@echo off

set FromDir=F:\FolderA\Export

set ToDir=F:\Folder\Outbound

set MyDate=%Date:~10,4%%Date:~4,2%%Date:~7,2%

set MyTime=%Time:~0,2%%Time:~3,2%%Time:~6,2%

move "%FromDir%\Vexport.txt" "%ToDir%\fedexport-%MyDate%-%MyTime%.txt"

end
=================================

I would guess it would need a If structure of some sort, but I dont know how to do that. It needs to process all of the files.

This is going to be on a Windows server 2003 standard box.

Thanks for your help!
POB-IT-DEPTAsked:
Who is Participating?
 
Bill PrewCommented:
Clearly trying to juggle too many things at once, sorry about that.  I see the problem, the following line:

   set Seq+=1

needs to be:

   set /a Seq+=1

~bp
0
 
Bill PrewCommented:
At this point do you even need the date stamping?  Why not just move exerything found to the destination folder with it's current (unique) name?

@echo off
set FromDir=F:\FolderA\Export
set ToDir=F:\Folder\Outbound
move "%FromDir%\Vexport*.txt" "%ToDir%"

Open in new window

~bp
0
 
POB-IT-DEPTAuthor Commented:
Unfortunately we do need that particular date time stamp.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
Bill PrewCommented:
Well, you will also need the "seq" number coming from the vendor, since there is a chance you could end up with the same date / time stamp on more than one file.  So I would just add on the incoming seq field to the date / time stamp to make sure unique.  Something like this:

(You could also move the MyDate and MyTime SET statements inside the loop over each file, but you will have to reference them with ! not % inside the loop. It depends if you want all the files in a "batch" to have the same time stamp or not?)

@echo off
setlocal EnableDelayedExpansion

set FromDir=F:\FolderA\Export
set ToDir=F:\Folder\Outbound

set MyDate=%Date:~10,4%%Date:~4,2%%Date:~7,2%
set MyTime=%Time:~0,2%%Time:~3,2%%Time:~6,2%

for %%A in ("%FromFolder%\vexport*.txt") do (
  set Seq=%%~nA
  set Seq=!Seq:~-12!
  move "%%~A" "%ToDir%\fedexport-%MyDate%-%MyTime%-!Seq!.txt"
)

Open in new window

~bp
0
 
POB-IT-DEPTAuthor Commented:
Referencing your first solution, just out of curiousity, I ran it and it gave me an error that states, "cannot move multiple files to a single file." If I can get the provider to give the file in an acceptable format the first solution might work in theory, but it does not work in practice.
0
 
Bill PrewCommented:
You will need to create the folder "F:\Folder\Outbound" before running that.  If it exists it should work without error.

~bp
0
 
POB-IT-DEPTAuthor Commented:
I found the error on my side. I had created the folder outbound under the same folderA directory. So my second set should have read set ToDir=F:\FolderA\Outbound

So that takes care of that.

On your second solution I would like it to take the individual file from export and move them to the outbound folder. I would want it to take the file Vexportbackup093020130830.txt and rename it to fedexport-date-timestamp and do that for each file in the export folder. If it needs to have additional information tagged on, it would be nice if it just tagged a 1 on the end for the first and 2 for the second, etc..
0
 
Bill PrewCommented:
Okay, here's an approach for that.

@echo off
setlocal EnableDelayedExpansion

set FromDir=F:\FolderA\Export
set ToDir=F:\Folder\Outbound

set MyDate=%Date:~10,4%%Date:~4,2%%Date:~7,2%
set MyTime=%Time:~0,2%%Time:~3,2%%Time:~6,2%
set Seq=0

for %%A in ("%FromFolder%\vexport*.txt") do (
  set Seq+=1
  move "%%~A" "%ToDir%\fedexport-%MyDate%-%MyTime%-!Seq!.txt"
)

Open in new window

~bp
0
 
POB-IT-DEPTAuthor Commented:
Bill,

The script put one of the files in the correct folder. I put a pause at the end of your script and it showed that all five of the files had moved, but only one was in the outbound folder. Inside the text files it was moving I put File # 1, File #2, etc. . That way I could determine the file it moved. It was file # 5. So it looks like it tries to move the files, but doesn't paste all of them to the folder.
0
 
Bill PrewCommented:
Sorry, small typo, change this line:

    for %%A in ("%FromFolder%\vexport*.txt") do (

to:

    for %%A in ("%FromDir%\vexport*.txt") do (

~bp
0
 
POB-IT-DEPTAuthor Commented:
I had already corrected the FromDir error. I still only get the last file in the list. The others are maybe being overwritten? It creates the file with the correct name, but it has the seq number as 0. So maybe it is overwritting it for each file moved? Not sure, but it indicates 5 have been moved before the pause, but only 1 file is in the outbound folder.
0
 
POB-IT-DEPTAuthor Commented:
That did the trick! Thanks very much!
0
 
POB-IT-DEPTAuthor Commented:
I appreciate the help!
0
 
Bill PrewCommented:
Great, glad we made some headway there, thanks for the feedback, and patience.

~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.