windows 将文件移动到新目录的批处理命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5588264/
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 08:15:18  来源:igfitidea点击:

Batch command to move files to a new directory

windowsbatch-filecmd

提问by Gabriel

I want to write a batch job that when executed will grab all the files in the C:\Test\Logfolder and move them to a new directory in the C:\Test. This new directory will have a name called "Backup-" and CURRENT DATE.

我想编写一个批处理作业,执行时将抓取文件C:\Test\Log夹中的所有文件并将它们移动到C:\Test. 这个新目录将有一个名为“Backup-”和当前日期的名称。

So once completed, the log folder should be empty with all the files now located in the new folder.

所以一旦完成,日志文件夹应该是空的,所有文件现在都位于新文件夹中。

I know I would have to use the MOVEcommand, but have no idea how to dynamically create a new folder, and use the date to name it.

我知道我将不得不使用该MOVE命令,但不知道如何动态创建一个新文件夹,并使用日期来命名它。

回答by Ken White

Something like this might help:

像这样的事情可能会有所帮助:

SET Today=%Date:~10,4%%Date:~4,2%%Date:~7,2%
mkdir C:\Test\Backup-%Today%
move C:\Test\Log\*.* C:\Test\Backup-%Today%\
SET Today=

The important part is the first line. It takes the output of the internal DATEvalue and parses it into an environmental variable named Today, in the format CCYYMMDD, as in '20110407`.

重要的部分是第一行。它获取内部DATE值的输出并将其解析为名为 的环境变量Today,格式CCYYMMDD为 '20110407'。

The %Date:~10,4%says to extract a *substring of the Dateenvironmental variable 'Thu 04/07/2011' (built in - type echo %Date%at a command prompt) starting at position 10 for 4 characters (2011). It then concatenates another substring of Date:starting at position 4 for 2 chars (04), and then concats two additional characters starting at position 7 (07).

%Date:~10,4%说,以提取的*子Date环境变量“星期四04/07/2011”(建于-型echo %Date%在提示的命令)起始于位置10为4个字符(2011)。然后它连接另一个Date:从位置 4 开始的子字符串用于 2 个字符 ( 04),然后连接两个从位置 7 开始的附加字符 ( 07)。

*The substring value starting points are 0-based.

*子串值的起始点是从 0 开始的。

You may need to adjust these values depending on the date format in your locale, but this should give you a starting point.

您可能需要根据您所在地区的日期格式调整这些值,但这应该为您提供一个起点。

回答by Nirmal- thInk beYond

this will also work, if you like

如果您愿意,这也将起作用

 xcopy  C:\Test\Log "c:\Test\Backup-%date:~4,2%-%date:~7,2%-%date:~10,4%_%time:~0,2%%time:~3,2%" /s /i
 del C:\Test\Log