问题
I have a list of files organized like this: test%MM%YYYY%DD.txt, for example:
test01201401.txt
test01201402.txt
test01201403.txt
...
test02201401.txt
test02201402.txt
...
I would like to create monthly-based folders like \test%MM%YYYY
(e.g. \test012014
or \test022014
) and then move all the daily based .txt files into the respective folder, for example all test012014*
files are moved to the \test012014
folder and all test022014*
files are moved to the \test022014
folder and so on.
Thanks!
回答1:
@echo off
setlocal enabledelayedexpansion
for /f %%f in ('dir /b ^| findstr /r "^test[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\.txt$"') do (
set "filename=%%~nf"
if not exist "!filename:~0,10!" md "!filename:~0,10!"
move "%%~f" "!filename:~0,10!"
)
For every filename that matches this regex ^test[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\.txt$
(a filename starting with test
, followed by 8 digits, and ending with .txt
), it checks if a folder whose name matches the first 10 characters of the filename (t e s t M M Y Y Y Y
) already exists, if not it creates it, then it moves the file there.
来源:https://stackoverflow.com/questions/29752371/batch-move-file-that-start-with-pattern-to-a-certain-folder