批处理文件以将具有相同开始字符串的文件移动到具有该字符串名称的文件夹中 [英] Batch File to Move Files with Same Beginning String to a Folder with That String Name

查看:166
本文介绍了批处理文件以将具有相同开始字符串的文件移动到具有该字符串名称的文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一组看起来像这样的文件:

So I have a set of files that look something like this:

Bob - Test Name - 1.txt
Bob - Test Name - 2.txt
Bob - Test Name - 3.txt
Sam- Foo Name - 1.txt
Sam- Foo Name - 2.txt
Sam- Foo Name - 3.txt
Phil - 1.txt
Phil - 2.txt
Phil - 3.txt
Phil 2 - 1.txt
Phil 2 - 2.txt
Phil 2 - 3.txt

简单来说,模式是这样的:

Simply, the pattern is something like this:

[WHATEVER] - #.txt
[WHATEVER](space)-(space)(number).extension

我想执行以下操作:

  1. 扫描文件夹
  2. 捕捉一切.在上面的示例中,捕获:"Bob-测试名称"
  3. 创建一个名为WHATEVER的文件夹.在这里,鲍勃-测试名称"
  4. 将所有具有WHATEVER字符串的文件移至文件夹.
  5. 然后在所有文件中重复2-4.

到目前为止,我尝试过的一切都失败了.不知道如何进行故障排除或查看正在执行的批处理,我在风中射击.

So far, everything I have tried, I have failed. Not knowing how to troubleshoot or see what batch is doing, I am shooting in the wind.

在这种情况下(在批处理文件的开头设置扩展名),我有各种文件类型(mp3等),因此我希望这与扩展名无关.

I would like this to be extension agnostic as I have various file types (mp3, etc) for this scenario (set extension at the beginning of the batch file).

推荐答案

@echo off &SETLOCAL
SET "startfolder=c:\test\test"

CD /d "%startfolder%"
FOR %%X IN (*.txt) DO (
    FOR /f "delims=" %%a IN ('echo(%%~NX^|sed -r "s/^(.*)\s-\s[0-9]+$/\1/"') DO (
    IF NOT EXIST "%%~a\" MD "%%~a"
    MOVE "%%~X" "%%~a"
    )
)

sed

这篇关于批处理文件以将具有相同开始字符串的文件移动到具有该字符串名称的文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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