所以我有一组看起来像这样的文件:
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
我想做以下事情:
到目前为止,我所尝试的一切,都失败了。我不知道如何排除故障或查看批次正在做什么,我正在风中拍摄。
我希望这是扩展无关的,因为我有这种情况的各种文件类型(mp3等)(在批处理文件的开头设置扩展名)。
答案 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"
)
)
答案 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"