Batch - Move file that start with pattern to a certain folder

大兔子大兔子 提交于 2020-01-06 13:11:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!