Search code examples
batch-filefile-copying

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

Solution

  • 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