我使用了下面的BAT脚本,但林不知道每行是干什么的。有人可以回答吗?
下面的bat脚本基本上读取目录中的最新文件,然后将其复制到另一个目录。我使用了下面的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(意味着05.20.2015)并删除字符串的其余部分(时间)。然后它使用转换后的日期格式将文件复制到P:\Stefan\
,这些文件在生成日期或之后被更改。
我会通过将:: explanation
放在它们上面来逐一解释这些行。
:: 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
我不认为这更简单,并且建立日期的方法是依赖于语言环境的,这意味着它可能无法在某些机器上工作,具体取决于日期格式。例如,它不适用于我的美国机器,日期格式为“Wed 05/20/2015”('Day MM/DD/YYYY') – dbenham
同意。我的评论更多地涉及'wmic'部分并选择最近的文件。需要使用日期标记的文件夹名称通常会消除'forfiles'命令的优势。如果我真的需要这两个功能,我会使用'robocopy'来代替(另一个提示OP)。 – user1016274
你描述你声称它复制最近修改的文件(单个文件)这纸条不起作用,但在现实中,它复制今天修改的所有文件(如果没有文件今天被修改,则无。 – dbenham