Copy File where filename contains a date

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
doctork11
Posts: 6
Joined: 17 Nov 2014 12:51

Copy File where filename contains a date

#1 Post by doctork11 » 17 Nov 2014 12:56

Each day I get a file delivered to a folder. I keep a history of all the files, so the folder is quite full. I need to figure out a way to copy the most recently delivered file to a new location. I was thing something like this:

Code: Select all

copy "\\Root\SharedFolder\File.xlsx" \\server\site\Folder\
, but I don't know where to put the wildcards and honestly, I don't think this will work (because the date will always be changing and I don't want all the files, just the most recent.

Also, there are several files in the folder that are not named the same. Example: file1_20141117, file2_20141117, etc. I need to find the most recent (file1) for example.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Copy File where filename contains a date

#2 Post by Squashman » 17 Nov 2014 13:14

Code: Select all

@echo off

PUSHD "\\Root\SharedFolder"

FOR /F "delims=" %%G in ('dir /B /OD File1*.xlsx') do "set newfile=%%~G"

copy "%newfile%" "\\server\site\Folder\"

POPD
Last edited by Squashman on 17 Nov 2014 14:07, edited 1 time in total.
Reason: My Bad. I forgot the /B switch.

doctork11
Posts: 6
Joined: 17 Nov 2014 12:51

Re: Copy File where filename contains a date

#3 Post by doctork11 » 17 Nov 2014 13:32

I keep getting an error message:


C:\Users\*>C:\Users\*\Desktop\test.bat
'"set newfile= Volume in drive O has no label."' is not recognized as an interna
l or external command,
operable program or batch file.
'"set newfile= Volume Serial Number is 9XXX-XXXX"' is not recognized as an inter
nal or external command,
operable program or batch file.
'"set newfile= Directory of O:\"' is not recognized as an internal or external c
ommand,
operable program or batch file.
The system cannot find the path specified.
The system cannot find the path specified.
'"set newfile= 2 File(s) 17,492 bytes"' is not recognized
as an internal or external command,
operable program or batch file.
'"set newfile= 0 Dir(s) 95,860,838,400 bytes free"' is not recogn
ized as an internal or external command,
operable program or batch file.
The system cannot find the path specified.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Copy File where filename contains a date

#4 Post by Squashman » 17 Nov 2014 14:06

doctork11 wrote:C:\Users\*>C:\Users\*\Desktop\test.bat

How are you trying to run the batch file? Very Odd to see Asterisks in an error message.

I would also like to see the actual batch file you are using. I want to see what you changed in it.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Copy File where filename contains a date

#5 Post by Squashman » 17 Nov 2014 14:08

I forgot the /B option for the DIR command. Recopy the batch file and try again.

doctork11
Posts: 6
Joined: 17 Nov 2014 12:51

Re: Copy File where filename contains a date

#6 Post by doctork11 » 17 Nov 2014 14:37

Asterisks were from me. I took out my name and replaced it. Sorry.

I made some changes and it works exactly how I want it to. Do you see any problem with the code?
P.S. I would have never gotten there without your help. Thank you

Code: Select all

@echo off

PUSHD "\\Computer\RPTest"

FOR /F "delims=|" %%I IN ('DIR "filename_*.xlsx" /B /O:D') DO SET newfile=%%I

copy "%newfile%" "\\Computer\RPView\"

POPD

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Copy File where filename contains a date

#7 Post by Squashman » 17 Nov 2014 14:43

I see no need for the pipe for the delimiter.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Copy File where filename contains a date

#8 Post by Compo » 17 Nov 2014 16:09

doctork11 wrote:Asterisks were from me. I took out my name and replaced it. Sorry.

I made some changes and it works exactly how I want it to. Do you see any problem with the code?
P.S. I would have never gotten there without your help. Thank you

Code: Select all

@echo off

PUSHD "\\Computer\RPTest"

FOR /F "delims=|" %%I IN ('DIR "filename_*.xlsx" /B /O:D') DO SET newfile=%%I

copy "%newfile%" "\\Computer\RPView\"

POPD

Try this!

Code: Select all

@PushD \\Computer\RPTest
@For /F "Delims=" %%A In ('Dir/B/OD "filename_*.xlsx"') Do @Set newfile="%%A"
@Copy %newfile% \\Computer\RPView

Post Reply