NonProgrammer
NonProgrammer

Reputation: 1387

Copy a file using .bat to another location

Is it possible to use a wildcard (instead of hard-coding entire file name) to copy files from one location to another? Also, I'd like to attach HHSS (hours and seconds) to the file name.

Example: Everyday, our system generates a few files with following name format:

GL_YYYYMMDD.txt
AP_YYYYMMDD.txt

I want to copy/move these files to another folder called "Backups" and attach HHSS (hours and seconds) so file name would look something like:

GL_YYYYMMDDHHSS
AP_YYYYMMDDHHSS

What I have so far:

Rem Determine date
Set mm1=%date:~4,2%
Set dd1=%date:~7,2%
Set yyyy1=%date:~10,4%

REM Determine Time
Set HH=%time:~0,2%
IF "%HH:~0,1%" == " " SET HH=0%HH:~1,1%
Set MM=%time:~3,2%
Set SEC=%time:~6,2%
Set runtime=%HH%%MM%%SEC%

rem Seconday date backup
cd E:\Blackline\DailyFiles

copy GL* E:\Blackline\Backups\"GL*%runtime%"

pause

Upvotes: 2

Views: 1468

Answers (2)

keatklein
keatklein

Reputation: 154

Try this. If you remove the .txt extensions, copy the files, then add the extension back you end up with your copied files retaining their original names with the added timestamp.

Rem Determine date
Set mm1=%date:~4,2%
Set dd1=%date:~7,2%
Set yyyy1=%date:~10,4%

REM Determine Time
Set HH=%time:~0,2%
IF "%HH:~0,1%" == " " SET HH=0%HH:~1,1%
Set MM=%time:~3,2%
Set SEC=%time:~6,2%
Set runtime=%HH%%MM%%SEC%

rem Seconday date backup
cd E:\Blackline\DailyFiles

rename *.txt *.

copy GL* E:\Blackline\Backups\GL*%runtime%.txt

rename *. *.txt


pause

Upvotes: 2

Steven K. Mariner
Steven K. Mariner

Reputation: 441

Star wildcards ( * ) in the destination filespec only work with an exact replication of the same columns of the filename from source to destination, terminated and restarted by the last dot ( . ) as that tradtionally represents the file type or "extension":

M:\t\a>dir
 Volume in drive M is MyDrive
 Volume Serial Number is ABCD-EF01

 Directory of M:\t\a

08/28/2017  05:42 PM    <DIR>          .
08/28/2017  05:42 PM    <DIR>          ..
08/28/2017  05:42 PM                17 test1.dat
08/28/2017  05:42 PM                17 test2.dat
08/28/2017  05:42 PM                17 test3.dat
               3 File(s)             51 bytes
               2 Dir(s)   1,050,894,336 bytes free

M:\t\a>copy test*.dat ..\b\test*.abc
test1.dat
test2.dat
test3.dat
        3 file(s) copied.

M:\t\a>dir ..\b
 Volume in drive M is MyDrive
 Volume Serial Number is ABCD-EF01

 Directory of M:\t\b

08/28/2017  05:44 PM    <DIR>          .
08/28/2017  05:44 PM    <DIR>          ..
08/28/2017  05:42 PM                17 test1.abc
08/28/2017  05:42 PM                17 test2.abc
08/28/2017  05:42 PM                17 test3.abc
               3 File(s)             51 bytes
               2 Dir(s)   1,050,894,336 bytes free

M:\t\a>

To insert additional components in the filename, you're going to need to get them one at a time. I recommend a FOR loop.

Side note: be cautious with possible spaces in your date and time extractions. For example, check that 9am is represented as "09:00" instead of " 9:00".

Here's a quick example of the FOR loop using your proposed mechanism for grabbing the time (which I did not confirm works in the morning):

@echo off
REM Determine Time
Set HH=%time:~0,2%
IF "%HH:~0,1%" == " " SET HH=0%HH:~1,1%
Set MM=%time:~3,2%
Set SEC=%time:~6,2%
Set runtime=%HH%%MM%%SEC%

for %%F in (test*.dat) do copy "%%F" "..\b\%%~nF%runtime%%%~xF"

Which yields the following results, appending time to the end of the test1/test2/test3 part of the filename:

M:\t\a>dir
 Volume in drive M is MyDrive
 Volume Serial Number is ABCD-EF01

 Directory of M:\t\a

08/28/2017  05:42 PM    <DIR>          .
08/28/2017  05:42 PM    <DIR>          ..
08/28/2017  05:51 PM               227 copywithtime.bat
08/28/2017  05:42 PM                17 test1.dat
08/28/2017  05:42 PM                17 test2.dat
08/28/2017  05:42 PM                17 test3.dat
               3 File(s)             51 bytes
               2 Dir(s)   1,050,894,336 bytes free

M:\t\a>copywithtime
        1 file(s) copied.
        1 file(s) copied.
        1 file(s) copied.

M:\t\a>dir ..\b
 Volume in drive M is MyDrive
 Volume Serial Number is ABCD-EF01

 Directory of M:\t\b

08/28/2017  05:44 PM    <DIR>          .
08/28/2017  05:44 PM    <DIR>          ..
08/28/2017  05:42 PM                17 test1175134.dat
08/28/2017  05:42 PM                17 test2175134.dat
08/28/2017  05:42 PM                17 test3175134.dat
               3 File(s)             51 bytes
               2 Dir(s)   1,050,894,336 bytes free

M:\t\a>

Upvotes: 2

Related Questions