在文件名+ Robocopy日志记录中使用当前日期创建文件 [英] Create File with current Date in Filename + Robocopy logging

查看:925
本文介绍了在文件名+ Robocopy日志记录中使用当前日期创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对Batchfile,脚本和一般的编码"经验很少,我很快就遇到了要创建的Batch的问题.

Having very little experience with Batchfiles, scripting and generally "coding", I quickly ran into a problem with a Batch I want to create.

情况如下:

我有一个文件夹,其中会自动插入* .txt文件,我想根据文件名将这些文件移动到其他文件夹中.我用Robocopy做到了,而且效果很好.然后,我发现可以记录Robocopy所做的工作的可能性. 批处理当前如下所示:

I have a folder, in which *.txt files are automatically inserted, and I wanted to move these files to different folders based on the names the files have. I did this with Robocopy and it works just fine. Then I discovered the possibility to log the stuff that Robocopy does. The Batch currently looks like this:

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计划任务的一部分,该任务必须每分钟运行一次.由于要移动的文件很多,因此日志文件很快就会变得很肿.现在,我每天都需要一个日志文件,同一批次在新的一天首次运行时会自动创建.当然,如果新创建的日志文件的名称包含创建日期,那将是完美的.我想将所有这些放在robocopy行之上. 用伪代码,我想要这样的东西:

This Batch has to be part of a Windows Scheduled Task, that has to run every Minute. Because there are a lot of files to be moved, the logfile will soon be very bloated. I now need a Logfile for every day, that the same Batch automatically creates the first time it runs on a new day. It would be perfect if the name of the newly created Logfile contained the Date it was created, of course. I want to put all of this above the robocopy lines. In pseudocode, I'd like to have something like this:

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

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

...if that makes any sense. I just don't know how to express that to work in a Batch.

推荐答案

该批次演示了如何使用wmi获取实际日期,并将ISO日期时间拆分为您可以根据自己的喜好拆分的部分.

This batch demonstrates how to get the actual date with wmi and it splits that ISO date time into parts you can assemble to your liking.

@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

这篇关于在文件名+ Robocopy日志记录中使用当前日期创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆