Batch file 创建文件夹从文件名torrent中提取年份,然后在中重新排列文件

Batch file 创建文件夹从文件名torrent中提取年份,然后在中重新排列文件,batch-file,Batch File,我有一个目录中的.torrent文件列表示例 Le Rane del Mare 1951.torrent Left Behind - La Profezia 2014.torrent Lo straordinario viaggio di T.S. Spivet.torrent Maleficent 2014 [dvd rip].torrent Mandingo (1975).torrent Maurice - Versione Integrale 1987.torrent 我想要一个.bat

我有一个目录中的.torrent文件列表示例

Le Rane del Mare 1951.torrent
Left Behind - La Profezia 2014.torrent
Lo straordinario viaggio di T.S. Spivet.torrent
Maleficent 2014 [dvd rip].torrent
Mandingo (1975).torrent
Maurice - Versione Integrale 1987.torrent
我想要一个.bat。此bat必须从文件名中提取年份来生成文件夹,如

1951
2014
1975
1987
然后我想在以前创建的文件夹中移动文件

Le Rane del Mare 1951 --> 1951
Left Behind - La Profezia 2014 --> 2014
Maleficent 2014 --> 2014
Mandingo 1975 --> 1975
Maurice - Versione Integrale 1987 --> 1987
我使用Win7

我使用此代码,但我必须先创建文件夹列表。我希望使用批处理创建,而不是手动创建

    @echo off
setlocal enabledelayedexpansion
pushd %1
for /F "USEBACKQ tokens=*" %%a in (`dir /b /a:-d`) do (
    set "_file=%%a"
    for /D %%b in (*) do (
        if NOT "x!_file:%%b=!" == "x!_file!" (
            move "%%a" "%%b"
        )
    )
)
popd

根据您最初提供的示例,这将完成:

@Echo Off
For %%a In (*19???.torrent *20???.torrent) Do Call :Sub "%%~a"
Exit/B
:Sub
Set "mfn=%~n1"
Set "lfc=%mfn:~-5%
Set "lfc=%lfc:(= %"
Set "lfc=%lfc:)= %"
Set "lfc=%lfc: =%"
If Not Exist "%lfc%\" MD %lfc%
Move %1 %lfc%
但是,您已将示例更改为包含年份之后的字符,此解决方案将不再适用于您。 不幸的是,如果没有看到包含所有torrent文件的列表,脚本将无法识别四位数字,其中只有两位已知,并根据您的要求移动它们

替代解决方案:

@Echo Off
For /F "Tokens=*" %%a In (
    'Dir/B/A-D *.torrent^|Findstr/R "\<19[0-9][0-9]\> \<20[0-1][0-9]\>"') Do (
    For %%b In (%%~na) Do Call :Sub "%%a" "%%b")
Pause
Exit/B
:Sub
Set "mfn=%~2"
Set "mfn=%mfn:(=%"
If %mfn:)=% GEq 1900 (If %mfn:)=% Lss 2017 (If Not Exist "%mfn:)=%\" MD %mfn:)=%
        Move %1 %mfn:)=%))
@Echo关闭
对于/F“令牌=*”%%a,在(
“Dir/B/A-D*.torrent^| Findstr/R”\\\”)做什么(
对于(%%~na)中的%%b,请调用:Sub“%%a”“%%b”)
暂停
退出/B
:Sub
设置“最惠国待遇=%~2”
设置“mfn=%mfn:(=%)
如果%mfn:)=%GEq 1900(如果%mfn:)=%Lss 2017(如果不存在”%mfn:)=%MD%mfn:)=%
移动%1%mfn:)=%)

I立即测试:您的解决方案很好,但您的脚本仅创建文件夹。是否可以将文件移到内部?我已经进行了编辑,使其现在可以执行移动,我还制作了一个替代版本,其中可能包含一些此版本未涉及的torrent文件。@Compo我喜欢第二个解决方案。我很惊讶第二个示例中的
findstr
搜索字符串能够工作,因为根据,单词边界(
\
)需要分别位于正则表达式的开头和结尾处。;无论如何,这是一个很好的解决方案!