批处理文件将具有相同起始字符串的文件移动到具有该字符串名称的文件夹

时间:2013-08-08 00:55:37

标签: batch-file

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

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

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

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

我想做以下事情:

  1. 扫描文件夹
  2. 捕获WHATEVER。在上面的示例中,捕获:“Bob - 测试名称”
  3. 创建名为WHATEVER的文件夹。这里,“鲍勃 - 测试名称”
  4. 将具有WHATEVER字符串的所有文件移动到文件夹。
  5. 并通过所有文件重复2-4。
  6. 到目前为止,我所尝试的一切,都失败了。我不知道如何排除故障或查看批次正在做什么,我正在风中拍摄。

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

3 个答案:

答案 0 :(得分:1)

@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

答案 1 :(得分:0)

我不完全确定你想要什么,但我认为这会奏效:

setlocal enabledelayedexpansion
cd C:\...[path to folder]
set /a count=0
for /r "delims=-" %%a in (*) do (
set /a count=!count!+1
for /l %%z in (1,1,!count!) do (
if %%a equ !%%z! ( move %%a C:\...[out put folder collection]\!%%z!\%%a%%b
) else ( set !count!=%%a)
)
)

输出

Folder Collection
 > 1
    >> name - 1.txt
    >> name - 2.txt
    >> name - 3.txt
 > 2
    >> name - 1.txt
    >> name - 2.txt
    >> name - 3.txt
 > 3
    >> name - 1.txt
    >> name - 2.txt
    >> name - 3.txt

我很确定这会奏效。没有测试过,所以如果你发现或得到任何错误告诉我。

注意:这只会在文件名中接受一个“ - ”。拥有多于一个意味着它将排除扩展。 另外,我没有让它适用于指定的扩展。再次回答,这次使用forfiles,您可以指定扩展名。

莫纳

答案 2 :(得分:0)

这段代码适用于与* - #.*匹配的文件,其中#是一个数字,*可能包含多个(space)-(space) - 仅限(space)-(space)(number).被记录了。

SETLOCAL ENABLEDELAYEDEXPANSION
REM Call process for each file that match "name - number.extension"
FOR %%f IN ("* - *.*") DO CALL :process "" "%%f"
GOTO :EOF

REM Recursive function process: accepts two arguments: prefix and filepart
REM process checks if number part of supplied filename is a number.
REM prefix is string already checked but mismatched number. Example:
REM Call 1: "" "a - b - c - 1.ext" mismatch ("b - c - 1" is not a number)
REM Call 2: "a -" " b - c - 1.ext" mismatch ("c - 1" is not a number)
REM Call 3: "a - b -" " c - 1.ext" match ("1" is a number)
:process
REM Break filepart at first "-"
FOR /F "TOKENS=1,* DELIMS=-" %%i IN (%2) DO (
    REM If after "-" is empty, file mismatch (something like "abc.ext-")
    IF "%%j" == "" GOTO :EOF
    REM Break part after "-" at first "."
    FOR /F "TOKENS=1,* DELIMS=." %%n IN ("%%j") DO (
        REM Multiply part before "." by 1. If part is not number, will evaluate to 0.
        SET /A "number=1*%%n"
        REM Check if rebuilt part afte "-" match using parsed number
        IF "%%j" == " !number!.%%o" (
            REM Matched, so do whatever is needed
            ECHO Moving "%~1%~2" to "%~1%%i"...
            IF NOT EXIST "%~1%%i" MD "%~1%%i"
            REM MOVE "%~1%~2" "%~1%%i"
            REM Exit function
            GOTO :EOF
        )

    )
    REM File part didn't match, so recall process shifting part before "-" to prefix
    REM and using part after "-" as filepart.
    CALL :process "%~1%%i-" "%%j"
)

它可能会泄漏,例如name1 - 1. name2 - 2.txt,创建文件夹name1而不是name1 - 1. name2

编辑:添加评论。修正了CALL :process "%~1%%i-" "%%j"

上的拼写错误