使用.bat将文件复制到另一个位置

时间:2017-08-28 21:25:11

标签: batch-file file-copying

是否可以使用通配符(而不是硬编码整个文件名)将文件从一个位置复制到另一个位置?另外,我想将HHSS(小时和秒)附加到文件名。

示例:我们的系统每天都会生成一些名称格式如下的文件:

GL_YYYYMMDD.txt
AP_YYYYMMDD.txt

我想将这些文件复制/移动到名为"备份"的另一个文件夹中。并附上HHSS(小时和秒),因此文件名看起来像:

GL_YYYYMMDDHHSS
AP_YYYYMMDDHHSS

到目前为止我所拥有的:

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

2 个答案:

答案 0 :(得分:2)

目标文件规范中的星型通配符(*)仅适用于从源到目标的文件名相同列的精确复制,由最后一个点(.)终止并重新启动传统上表示文件类型或"扩展名":

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>

要在文件名中插入其他组件,您需要一次获取一个组件。我建议使用FOR循环。

附注:在提取日期和时间时要小心谨慎。例如,检查9am是否代表"09:00"而不是" 9:00"

以下是FOR循环的一个简单示例,使用您建议的抓取时间的机制(我做了确认在早上工作):

@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"

产生以下结果,将时间添加到文件名的test1 / test2 / test3部分的末尾:

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>

答案 1 :(得分:2)

试试这个。如果删除.txt扩展名,请复制文件,然后添加扩展名,最后复制的文件保留其原始名称和添加的时间戳。

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