使用 .bat 将文件复制到另一个位置
Copy a file using .bat to another location
是否可以使用通配符(而不是对整个文件名进行硬编码)将文件从一个位置复制到另一个位置?另外,我想将 HHSS(小时和秒)附加到文件名。
示例:我们的系统每天都会生成一些具有以下名称格式的文件:
GL_YYYYMMDD.txt
AP_YYYYMMDD.txt
我想 copy/move 这些文件到另一个名为 "Backups" 的文件夹并附加 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
目标文件规范中的星号通配符 ( *
) 仅适用于从源到目标的文件名相同列的精确复制,由最后一个点 ( .
) 终止和重新启动因为传统上代表文件类型或 "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>
要在文件名中插入其他组件,您需要一次一个地获取它们。我推荐 FOR
循环。
旁注:注意日期和时间提取中可能存在的空格。例如,检查上午 9 点是否表示为 "09:00"
而不是 " 9:00"
.
这是一个 FOR
循环的简单示例,它使用您提出的机制来抓紧时间(我在早上 not 确认工作):
@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>
试试这个。如果您删除 .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
是否可以使用通配符(而不是对整个文件名进行硬编码)将文件从一个位置复制到另一个位置?另外,我想将 HHSS(小时和秒)附加到文件名。
示例:我们的系统每天都会生成一些具有以下名称格式的文件:
GL_YYYYMMDD.txt
AP_YYYYMMDD.txt
我想 copy/move 这些文件到另一个名为 "Backups" 的文件夹并附加 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
目标文件规范中的星号通配符 ( *
) 仅适用于从源到目标的文件名相同列的精确复制,由最后一个点 ( .
) 终止和重新启动因为传统上代表文件类型或 "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>
要在文件名中插入其他组件,您需要一次一个地获取它们。我推荐 FOR
循环。
旁注:注意日期和时间提取中可能存在的空格。例如,检查上午 9 点是否表示为 "09:00"
而不是 " 9:00"
.
这是一个 FOR
循环的简单示例,它使用您提出的机制来抓紧时间(我在早上 not 确认工作):
@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>
试试这个。如果您删除 .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