0

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

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. 捕捉任何东西。在上面的示例中,捕获:“Bob - 测试名称”
  3. 创建名为 WHATEVER 的文件夹。在这里,“Bob - 测试名称”
  4. 将所有具有 WHATEVER 字符串的文件移动到文件夹。
  5. 并在所有文件中重复 2-4。

到目前为止,我尝试过的一切都失败了。不知道如何排除故障或查看批次在做什么,我在风中射击。

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

4

3 回答 3

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

于 2013-08-08T05:59:17.553 回答
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可以指定扩展名的地方。

莫娜

于 2013-08-08T02:07:57.323 回答
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"

于 2013-08-08T09:38:32.893 回答