move and rename FIRST file in directory

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
thebody
Posts: 1
Joined: 09 Mar 2009 03:20

move and rename FIRST file in directory

#1 Post by thebody » 09 Mar 2009 03:28

Hoping someone can help cause I've wasted ALOT of time on this.

I have a directory that downloads a number of reports. These each take the same format: CDR_yyyymmdd.csv

I want to be able to access the FIRST file in a directory that matchese that pattern(just CDR*.csv will do and first copy it to another directory and then rename it.

For the record another program comes along and processes the renamed file and then deletes it, so I can only process one file per sweep.

Thanks for any help.

RElliott63
Expert
Posts: 80
Joined: 04 Feb 2009 10:03

#2 Post by RElliott63 » 09 Mar 2009 07:48

Here's a start for you... this also sorts them in File Name Order. If you don't need to sort, then remove the /ON from the DIR command. It will only move the first file, then get out. Otherwise, if you need to move more than one, you'll have to remove the Goto GetOut.

Code: Select all

 
For /F "Delims=" %%F In ('Dir /B/ON CDR_*.csv') Do (
     
     Copy /v %%F \NewLoc\NewName.ext
     If Exist \NewLoc\NewName.ext (
        Erase %%F
        Echo Move of File %%F was Successful!
     )  Else (
        Echo Error on Moving file %%F!
     )
     
     Goto GetOut
 )

:GetOut
 Echo;
 Echo Finished Moving file Job
 

Post Reply