我有大约 100,000 个文件,其名称如下:
completedCourses_2017-10-08 11 18-05-02.txt
或者emptyCoursesResults_2017-10-08 11 13-21-59.txt
所有这些文件都在一个目录中,我想按日期将其“拆分”到目录中,例如,所有带有日期的文件2017-10-08
进入带有名称的目录2017-10-08
,带有日期的文件2018-01-02
进入带有名称的目录2018-01-02
- 无论名称的开头和结尾是什么。
是否可以使用 Windows 解决方案?
答案1
您可以使用为/F循环并设置"USEBACKQ TOKENS=2 DELIMS=_ "
类似的内容来从文件名中解析并获取日期字符串,以便在后续命令中重新使用,以执行您需要的操作。
然后,您将使用多种命令(例如DIR
、、和)执行文件操作MD
,以将其移动到新的日期字符串文件夹。XCOPY
DEL
笔记: 确保将Src=
变量值设置为这些文件所在的文件夹,并确保将Dest=
变量值设置为文件将被移动到的文件夹,并且yyyy-mm-dd
将为移动后这些文件创建的文件夹。
@ECHO ON
SET "Src=C:\Folder\Source"
SET "Dest=C:\Folder\Destination"
FOR /F "USEBACKQ TOKENS=2 DELIMS=_ " %%A IN (`DIR /B "%Src%\*.txt"`) DO (
IF NOT EXIST "%%~A" MD "%%~A"
XCOPY /F /Y "%Src%\*%%~A*.txt" "%Dest%\%%~A\" && DEL /Q /F "%Src%\*%%~A*.txt"
)
EXIT
更多资源
- 为/F
FOR /?
delims=xxx - specifies a delimiter set. This replaces the default delimiter set of space and tab. tokens=x,y,m-n - specifies which tokens from each line are to be passed to the for body for each iteration. This will cause additional variable names to be allocated. The m-n form is a range, specifying the mth through the nth tokens. If the last character in the tokens= string is an asterisk, then an additional variable is allocated and receives the remaining text on the line after the last token parsed. usebackq - specifies that the new semantics are in force, where a back quoted string is executed as a command and a single quoted string is a literal string command and allows the use of double quotes to quote file names in file-set.
- 德尔
- 目录