Link to home
Start Free TrialLog in
Avatar of POB-IT-DEPT
POB-IT-DEPT

asked on

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!
Avatar of Bill Prew
Bill Prew

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
Avatar of POB-IT-DEPT

ASKER

Unfortunately we do need that particular date time stamp.
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
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.
You will need to create the folder "F:\Folder\Outbound" before running that.  If it exists it should work without error.

~bp
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..
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
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.
Sorry, small typo, change this line:

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

to:

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

~bp
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.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That did the trick! Thanks very much!
I appreciate the help!
Great, glad we made some headway there, thanks for the feedback, and patience.

~bp