Page 1 of 1

Copy File where filename contains a date

Posted: 17 Nov 2014 12:56
by doctork11
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.

Re: Copy File where filename contains a date

Posted: 17 Nov 2014 13:14
by Squashman

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

Re: Copy File where filename contains a date

Posted: 17 Nov 2014 13:32
by doctork11
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.

Re: Copy File where filename contains a date

Posted: 17 Nov 2014 14:06
by Squashman
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.

Re: Copy File where filename contains a date

Posted: 17 Nov 2014 14:08
by Squashman
I forgot the /B option for the DIR command. Recopy the batch file and try again.

Re: Copy File where filename contains a date

Posted: 17 Nov 2014 14:37
by doctork11
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

Re: Copy File where filename contains a date

Posted: 17 Nov 2014 14:43
by Squashman
I see no need for the pipe for the delimiter.

Re: Copy File where filename contains a date

Posted: 17 Nov 2014 16:09
by Compo
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