繁体   English   中英

批量 - 将以 pattern 开头的文件移动到某个文件夹

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

我有一个这样组织的文件列表:test%MM%YYYY%DD.txt,例如:

test01201401.txt
test01201402.txt
test01201403.txt
...
test02201401.txt
test02201402.txt
...

我想创建基于月的文件夹,如\test%MM%YYYY (例如\test012014\test022014 ),然后将所有每日 based.txt 文件移动到相应的文件夹中,例如所有test012014*文件都移动到\test012014文件夹和所有test022014*文件都移动到\test022014文件夹,依此类推。 谢谢!

@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!"
)

对于与此正则表达式匹配的每个文件名^test[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\\.txt$ (以test开头的文件名,后跟8位数字,以.txt结尾),它将检查是否存在名称与文件名的前10个字符匹配的文件夹( test MMYYYY ),如果不存在,则创建该文件夹,然后将文件移到那里。

例子

p:\DANE\telefony\Marcin\Archiwum>..\..\move-archive.bat
move IMG-20220119-WA0000.jpg 2022\2022-01\
move IMG-20220119-WA0001.jpg 2022\2022-01\

代码

@echo off
rem not %var% but !var! give us access to internal loop variable value with enabledelayedexpansion.
setlocal enabledelayedexpansion
rem Run by scheduler script in p:\dane\telefony\marcin\move-archive.bat
rem p:
rem cd p:\dane\telefony\marcin\archiwum
FOR %%V IN (*.*) DO (
rem echo "****** %%V *********"
SET filedate=%%~tV
rem echo !filedate!
SET fileyear=!filedate:~6,4!
rem echo !fileyear!
SET filemonth=!filedate:~3,2!
rem echo !filemonth!
rem create directory as yyyy\yyyy-MM
mkdir !fileyear!\!fileyear!-!filemonth! 2>nul
echo move %%V !fileyear!\!fileyear!-!filemonth!\
move %%V !fileyear!\!fileyear!-!filemonth!\ >nul
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM