Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 我使用了下面的BAT脚本,但我不确定每一行的作用。有人能回答吗?_Batch File_Copy - Fatal编程技术网

Batch file 我使用了下面的BAT脚本,但我不确定每一行的作用。有人能回答吗?

Batch file 我使用了下面的BAT脚本,但我不确定每一行的作用。有人能回答吗?,batch-file,copy,Batch File,Copy,下面的bat脚本基本上获取一个目录中的最新文件,然后将其复制到另一个目录 我想知道每一步具体做什么,因为我不熟悉脚本 谢谢 @echo off for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set "dt=%%a" set "YYYY=%dt:~0,4%" set "MM=%dt:~4,2%" set "DD=%dt:~6,2%" set datestamp=%MM%-%DD%-%YYYY%

下面的bat脚本基本上获取一个目录中的最新文件,然后将其复制到另一个目录

我想知道每一步具体做什么,因为我不熟悉脚本

谢谢

@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set     "dt=%%a"
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"

set datestamp=%MM%-%DD%-%YYYY%

XCOPY J:\vch\vch_soh_*.csv P:\Stefan\ /S /D:%datestamp%

命令
wmic OS Get localdatetime
返回如下时间表示:

LocalDateTime

20150520100512.927000+120

脚本将其转换为MM.DD.YYYY(表示2015年5月20日)的表示形式,并删除字符串的其余部分(时间)。然后,它使用转换后的日期格式将文件
J:\vch\vch\u soh.*.csv
复制到
P:\Stefan\
,这些文件在生成日期时或之后更改

我将通过在上面放置
::explainion
来逐一解释这些行

:: disable output of the code itself
@echo off

:: execute wmic OS Get localdatetime and store the line containing "." in the variable dt
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set "dt=%%a"

:: store the first 4 characters of dt in the variable YYYY
set "YYYY=%dt:~0,4%"
:: store the positions 5 and 6 of dt in the variable MM
:: caution! in ~4,2 "4" means positions 5 (index starts at 0)
:: and 2 is the number of positions to take, so ~4,2 means positions 5 and 6
set "MM=%dt:~4,2%"
:: save positions 7 and 8 in the variable DD
set "DD=%dt:~6,2%"

:: creates variable datastamp putting the variables from above together
:: and generates the string MM-DD-YYYY
:: this is needed because xcopy requires date in this format
set datestamp=%MM%-%DD%-%YYYY%

:: copy files created at the generated date or later
XCOPY J:\vch\vch_soh_*.csv P:\Stefan\ /S /D:%datestamp%

实际上,该脚本将今天创建的所有文件从X复制到Y。

有一种简单得多的方法:

forfiles /P J:\vch\ /M vch_soh_*.csv /D +0 /C "cmd /c copy @file destdir"
/p
设置源文件夹,
/S
也意味着扫描子文件夹,
/D+0
选择今天修改的文件。您可以在命令中使用其他占位符,请参见
forfiles/?
。是的,如果您需要目标文件夹名称为今天的日期,那么您可以使用

for /f "tokens=1-3 delims=." %i in ('date /T') do set ddir=%j-%i-%k

这个脚本不起作用,正如你所说的,它要求复制最近修改过的文件(单个文件),但实际上它复制了今天修改过的所有文件(如果没有文件被修改的话,今天就没有了)。我不认为这更简单,而且你建立日期的方法是与区域相关的。这意味着它可能无法在某些机器上工作,具体取决于日期格式。例如,如果日期格式为2015年5月20日星期三(
日MM/DD/YYYY
),则它在我的美国机器上不起作用。我的评论更多地是指
wmic
部分和选择最新文件。需要带日期戳的foldername通常会消除
forfiles
命令的优势。如果我真的需要这两种功能,我会使用
robocopy
(对OP的另一个提示)。