批处理-将以模式开头的文件移动到某个文件夹 [英] Batch - Move file that start with pattern to a certain folder

查看:149
本文介绍了批处理-将以模式开头的文件移动到某个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
...

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

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!

推荐答案

@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个字符匹配的文件夹(t e s t M M Y Y Y Y)已经存在,如果没有创建它,那么它将文件移到那里.

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.

这篇关于批处理-将以模式开头的文件移动到某个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆