windows 批处理文件按修改日期移动文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8488363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:00:37  来源:igfitidea点击:

Batch file to move files by date modified

windowsbatch-file

提问by tob88

I have written a batch file, which creates empty folders for each date. My next task is to create another batch file, which moves each file in a directory, into the relevant date folder, based on their date modified. I have read numerous forums and articles on how I can achieve this, but with my limited batch file knowledge, I just cannot seem to get it to work. The code I currently have is shown below, though this does not seem to pull in the date modified. Any help is much appreciated!

我写了一个批处理文件,它为每个日期创建空文件夹。我的下一个任务是创建另一个批处理文件,根据修改日期将目录中的每个文件移动到相关的日期文件夹中。我已经阅读了许多关于如何实现这一目标的论坛和文章,但由于我有限的批处理文件知识,我似乎无法让它工作。我目前拥有的代码如下所示,尽管这似乎没有引入修改日期。任何帮助深表感谢!

SET directory="\directory\path\archive"

FOR /f %%a in ('dir /b "%directory%"') do (

SET fdate=%%~Ta

MOVE "%directory%\%%a" "%directory%\%fdate%"

回答by dbenham

Until you provide more info as to format of dates, I can't give a definitive answer. But I can show you how I would do it on my machine.

在您提供有关日期格式的更多信息之前,我无法给出明确的答案。但我可以告诉你我将如何在我的机器上做到这一点。

I use yyyy-mm-dd format within file and folder names, so December 13, 2011 would be 2011-12-13. My machine uses mm/dd/yyyy format for dates (12/13/2011). So I would need to translate the %%~tF output from 12/13/2011 into 2011-12-13. Note - /cannot be used used in file or folder names.

我在文件和文件夹名称中使用 yyyy-mm-dd 格式,因此 2011 年 12 月 13 日将是 2011-12-13。我的机器对日期使用 mm/dd/yyyy 格式(12/13/2011)。所以我需要将 %%~tF 输出从 12/13/2011 转换为 2011-12-13。 注意 -/不能用于文件或文件夹名称。

So this code would do what you want on my machine:

所以这段代码会在我的机器上做你想做的事:

set "source=\directory\path\archive"
set "targetRoot=\directory\path\archive"
for %%F in ("%source%\*") do (
  for /f "tokens=1,2,3 delims=/ " %%A in ("%%~tF") do (
    move "%%~fF" "%targetRoot%\%%C-%%A-%%B"
  )
)

Addendum- Question in comment asks for method to left pad a number with zeros for dir creation. I see two easy choices. (This really should be a different question)

附录- 评论中的问题要求使用零填充数字以创建目录的方法。我看到两个简单的选择。(这真的应该是一个不同的问题)

This first method is simple but tedious and not practical as a general solution

第一种方法简单但乏味,作为通用解决方案并不实用

for %%A in (01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31) do ...

The second method is a general solution. Since your assignment is within parentheses, you need to use delayed expansion.

第二种方法是通用解决方案。由于您的赋值在括号内,您需要使用延迟扩展。

setlocal enableDelayedExpansion
for /l %%A in (1 1 31) do (
  set "day=0%%A"
  set "day=!day:~-2!
  ...
)

You increase the number of leading zeros by adding more 0 to the front and then increasing the number of characters you preserve in the substring operation.

您可以通过在前面添加更多 0,然后增加在子字符串操作中保留的字符数来增加前导零的数量。

BUT - why prepopulate the directories? Your strategy will add directory days that don't exist in the calendar, plus you are likely to have many unused folders for which no files were modified on that day. Better to create the folders only as needed. Then the 0 padding is already done for you, and no unneeded folders are created.

但是 - 为什么要预先填充目录?您的策略将添加日历中不存在的目录天数,而且您可能有许多未使用的文件夹,当天未对其文件进行修改。最好仅根据需要创建文件夹。然后 0 填充已经为您完成,并且不会创建不需要的文件夹。

set "source=\directory\path\archive"
set "targetRoot=\directory\path\archive"
for %%F in ("%source%\*") do (
  for /f "tokens=1,2,3 delims=/ " %%A in ("%%~tF") do (
    if not exist "%targetRoot%\%%C\%%A\%%B" mkdir "%targetRoot%\%%C\%%A\%%B"
    move "%%~fF" "%targetRoot%\%%C\%%A\%%B"
  )
)