Copy files with dynamic names

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mwatsondhs
Posts: 9
Joined: 13 Jan 2015 09:51

Copy files with dynamic names

#1 Post by mwatsondhs » 13 Jan 2015 10:04

I need to automate the following process:

A Crystal report is run on a monthly basis which creates the following file each month:

sls316_2015-01-06-04-12-37.pdf where 2015-01-06-04-23-37 is the date-time the pdf file was created.

Each month the date time changes and I need a batch script to copy this file to another directory.

Can I automate this with a .bat file?

Thanks.
Mark

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

Re: Copy files with dynamic names

#2 Post by Squashman » 13 Jan 2015 10:18

Well this could be approached a couple of different ways but really need more information.
1) Are you just trying to synchronize two folders for backup purposes. The PDF should exist in both folders.
or
2) Will this be the only PDF in the folder that needs to be copied and then it is deleted from that original location?
or
3) Does the file name always start with sls316?

mwatsondhs
Posts: 9
Joined: 13 Jan 2015 09:51

Re: Copy files with dynamic names

#3 Post by mwatsondhs » 13 Jan 2015 13:41

Just copying .pdf files from a report server to a shared network drive. The timestamp is written into the file name and
only need to copy the latest dated file. There will be several files that need to be copied like this but if you could show me an
example for 1 I could do the others.

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

Re: Copy files with dynamic names

#4 Post by Squashman » 13 Jan 2015 14:29

Could you please answer question 3.

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

Re: Copy files with dynamic names

#5 Post by Squashman » 13 Jan 2015 14:38

If you want the newest PDF file in the directory you can do this.

Code: Select all

FOR /F "delims=" %%G in ('dir *.pdf /a-d /b /od') do set "newest=%%~G"
copy "%newest%" "X:\path to some folder\"

mwatsondhs
Posts: 9
Joined: 13 Jan 2015 09:51

Re: Copy files with dynamic names

#6 Post by mwatsondhs » 13 Jan 2015 15:12

Squashman,

Thank You very much, that worked perfectly.

Mark

Post Reply