在文件名 + Robocopy 日志记录中创建具有当前日期的文件

Create File with current Date in Filename + Robocopy logging

对批处理文件、脚本和一般 "coding" 的经验很少,我很快 运行 遇到了我想创建的批处理的问题。

情况如下:

我有一个文件夹,其中会自动插入 *.txt 文件,我想根据文件的名称将这些文件移动到不同的文件夹。我用 Robocopy 做了这个,它工作得很好。然后我发现可以记录 Robocopy 所做的事情。 该批次目前看起来像这样:

robocopy C:\Source C:\Target_Normal file*.txt /xf file022*.txt /mov /log+:LogNo.txt /ns /nc /np /r:1 /w:5
robocopy C:\Source C:\Target_Special file022*.txt /mov /log+:LogNo.txt /ns /nc /np /r:1 /w:3

此批处理必须是 Windows 计划任务的一部分,该任务必须每分钟 运行。因为要移动的文件很多,logfile很快就会非常臃肿。我现在每天都需要一个日志文件,同一个批次在新的一天 运行 第一次自动创建。当然,如果新创建的日志文件的名称包含它的创建日期,那将是完美的。我想把所有这些都放在 robocopy 行之上。 在伪代码中,我想要这样的东西:

If currentDay has no Logfile yet -> 
  Create Logfile with Name Log+currentDate  -> 
else (nothing and continue?)

...如果有任何意义的话。我只是不知道如何表达它以批量工作。

这批演示了如何使用 wmi 获取实际日期,并将 ISO 日期时间拆分为您可以assemble 喜欢的部分。

@Echo off
SetLocal EnableExtensions EnableDelayedExpansion
:: Get Local date time 
for /f "tokens=1-3 delims=.+-" %%A in (
  'wmic os get LocalDateTime^|findstr ^^[0-9]'
    ) do Set _DT=%%A
Set "_yy=%_DT:~0,4%" & Set "_MM=%_DT:~4,2%"  & Set "_dd=%_DT:~6,2%"
Set "_hh=%_DT:~8,2%" & Set "_nn=%_DT:~10,2%" & Set "_ss=%_DT:~12,2%"
:: Put your date time elements together
::            %_DT:~0,8%  is yyyyMMdd
Set LogNo=Log_%_DT:~0,8%.txt
set _

Echo Logfile is : %LogNo%
Pause

robocopy C:\Source C:\Target_Normal file*.txt /xf file022*.txt /mov /log+:%LogNo% /ns /nc /np /r:1 /w:5
robocopy C:\Source C:\Target_Special              file022*.txt /mov /log+:%LogNo% /ns /nc /np /r:1 /w:3