按数字顺序排序文件

时间:2015-04-10 03:14:25

标签: batch-file vbscript

我有一个create shortcut based on the order of files的批次,问题是当涉及到数字时,他在传递数字100时会出现以下问题。

01.mp4
02.mp4
03.rmvb
04.mp4
05.rmvb
06.rmvb
07.rmvb
08.rmvb
09.rmvb
10.rmvb
100.mp4
101.mp4
102.mp4
103.mp4
104.mp4
105.mp4
106.mp4
107.mp4
108.mp4
109.mp4
11.rmvb

我在这里搜索并发现了各种方法,但我使用的脚本适用于文件夹和文件,有时使用重音& 和/或

Example: C:\Séries & Movies\Remix!.mkv (Brazil and use E place of and).

我想知道是否有任何方法可以检查内容并在将其保存在.ini中之前或在将其保存在.ini中之后进行组织。

观察:

  1. 第一次设置命令时加载文件夹路径。
  2. 输入路径后,他保存在.ini文件中并始终加载。
  3. 脚本列表中仅目录中的文件不会列出子文件夹及其中的文件和文件夹。
  4. 脚本需要其他文件才能工作下载链接如下: https://www.mediafire.com/?zcoybkfo8k4nm1t
  5. 我的完整代码:

    @Echo off
    
    Title Create shortcuts in alphabetical order
    mode con:lines=3 cols=25
    Color 1f
    
    CD /D "%~dp0"
    
    If Exist "Files\command.ini" For /f "usebackq delims=" %%x in ("Files\command.ini") do (set "%%x")
    If Exist "Files\Config.ini" For /f "usebackq delims=" %%x in ("Files\Config.ini") do (set "%%x")
    If Exist "Files\Files.ini" Goto shortcuts
    If Exist "Files\command.ini" Goto shortcuts
    If Exist "Files\Config.ini" Goto shortcuts
    
    for %%F in (""%1"") do Set "location-of-files=%%~F"
    for %%F in ("%location-of-files%") do IF "%%~F" NEQ """" Set "location-of-files=%location-of-files:"=%" & Set Number=1 & Goto LocationofFiles2
    
    :LocationofFiles
    mode con:lines=18 cols=78
    Set "location-of-files=r1u4unoiwqa6">nul 2>&1
    cls
    echo Location of Files
    Set /p location-of-files="¯ Location of Files: " 
    Set "location-of-files=%location-of-files:"=%"
    Set Number=1
    IF "%location-of-files%"=="r1u4unoiwqa6" Goto LocationofFiles
    
    
    :LocationofFiles2
    mode con:lines=18 cols=78
    Set "Menu=">nul 2>&1
    cls
    for %%F in ("%location-of-files%") do Echo %%~F
    echo 1(Yes) 2(No)
    Set/p Menu="¯ Menu: "
    IF "%Menu%"=="1" Goto Iniciar
    IF "%Menu%"=="2" Goto LocationofFiles
    Goto LocationofFiles2
    
    :Iniciar
    if not exist "%location-of-files%" Cls & Start /Wait Files\Error.vbs & Goto LocationofFiles
    
    :Name-AnimeSerie1
    Set "Serie_Anime=">nul 2>&1
    cls
    echo Name Serie
    Set /p Serie_Anime="¯ Name: " 
    IF "%Serie_Anime%"=="" Goto Name-AnimeSerie1
    
    
    :Name-AnimeSerie2
    Set "Menu=">nul 2>&1
    cls
    for %%F in ("%Serie_Anime%") do Echo %%~F
    echo 1(Yes) 2(No)
    Set/p Menu="¯ Menu: "
    IF "%Menu%"=="1" Goto shortcuts
    IF "%Menu%"=="2" Goto Name-AnimeSerie1
    Goto Name-AnimeSerie2
    
    :shortcuts
    If Exist "Files\Config.ini" For /f "usebackq delims=" %%x in ("Files\Config.ini") do (set "%%x")
    If Not Exist "%location-of-files%" Del /q "C:\Users\%username%\Desktop\%ep2% - %Serie_Anime%.lnk">nul 2>&1 & Start /Min /Wait Files\DesktopRefresh.exe>nul 2>&1 & Goto end
    Dir /a-d /b "%location-of-files%" >Files\Files.ini
    Echo r1u4unoiwqa6.ending >>Files\Files.ini
    
    Start "exclamation01" /Min /Wait "Files\exclamation01.vbs">nul 2>&1
    
    Set location-of-files > Files\Config.ini
    Set Serie_Anime >> Files\Config.ini
    Set Number > Files\command.ini
    
    If Exist "C:\Users\%username%\Desktop\%ep2% - %Serie_Anime%.lnk" Del /q "C:\Users\%username%\Desktop\%ep2% - %Serie_Anime%.lnk">nul 2>&1 & Start /Min /Wait Files\DesktopRefresh.exe>nul 2>&1
    
    setlocal EnableDelayedExpansion
    
    For /f "usebackq delims=" %%x in ("Files\command.ini") do (set "%%x")
    For /f "usebackq delims=" %%x in ("Files\Config.ini") do (set "%%x")
    
    set "cmd=findstr /R /N "^^" Files\Files.ini | find /C ":""
    
    for /f %%a in ('!cmd!') do set Numbers=%%a
    
    set lines=%Number%
    set Atual=1
    for /f "delims=" %%a in ('type Files\Files.ini') do (
        for %%b in (!lines!) do (
            if !Atual!==%%b Set "Ep1=%%a"
        )
        set /a "Atual = Atual + 1"
    )
    
    Set "Ep2=%Ep1%"
    set "find=*."
    call set delete=%%Ep2:!find!=%%
    call set Ep2=%%Ep2:!delete!=%%
    Set Ep2=%Ep2:.=%
    
    Set Ep1 > Files\command.ini
    Set Ep2 >> Files\command.ini
    Set lines >> Files\command.ini
    Set Number >> Files\command.ini
    
    endlocal
    
    Start "exclamation02" /Min /Wait "Files\exclamation02.vbs">nul 2>&1
    
    For /f "usebackq delims=" %%x in ("Files\command.ini") do (set "%%x")
    For /f "usebackq delims=" %%x in ("Files\Config.ini") do (set "%%x")
    
    IF "%Ep2%"=="r1u4unoiwqa6" Goto end
    
    Start /Min /Wait Files\Shortcut.exe /F:"C:\Users\%username%\Desktop\%ep2% - %Serie_Anime%.lnk" /A:C /t:"%location-of-files%\%Ep1%" /D:"Episode %Serie_Anime%">nul 2>&1
    If Not Exist "C:\Users\%username%\Desktop\[ shortcuts ].lnk" Echo %Serie_Anime%>Files\shortcut.ini & Start /Min /Wait Files\shortcut.vbs>nul 2>&1
    Set /A Number = %lines% + 1
    :::::::::::::::::::::::::::::::::::::::::::::
    Set location-of-files > Files\Config.ini
    Set Serie_Anime >> Files\Config.ini
    :::::::::::::::::::::::::::::::::::::::::::::
    Set Ep1 > Files\command.ini
    Set Ep2 >> Files\command.ini
    Set Number >> Files\command.ini
    :::::::::::::::::::::::::::::::::::::::::::::
    Exit
    
    :end
    If Not Exist "%location-of-files%" Start /Wait Files\PDoM.vbs>nul 2>&1
    If Exist "%location-of-files%" Start /Wait Files\ending.vbs>nul 2>&1
    If Exist "%location-of-files%" Start "Anime" "%location-of-files%">nul 2>&1
    Del /q "Files\Files.ini">nul 2>&1
    Del /q "Files\shortcut.ini">nul 2>&1
    Del /q "Files\command.ini">nul 2>&1
    Del /q "Files\Config.ini">nul 2>&1
    Set "location-of-files=">nul 2>&1
    Set "Serie_Anime=">nul 2>&1
    Set "lines=">nul 2>&1
    Set "Ep1=">nul 2>&1
    Set "Ep2=">nul 2>&1
    Goto LocationofFiles
    

    您需要使用DIR的部分:

    :shortcuts
    If Exist "Files\Config.ini" For /f "usebackq delims=" %%x in ("Files\Config.ini") do (set "%%x")
    If Not Exist "%location-of-files%" Del /q "C:\Users\%username%\Desktop\%ep2% - %Serie_Anime%.lnk">nul 2>&1 & Start /Min /Wait Files\DesktopRefresh.exe>nul 2>&1 & Goto end
    Dir /a-d /b "%location-of-files%" >Files\Files.ini
    Echo r1u4unoiwqa6.ending >>Files\Files.ini
    
    Start "exclamation01" /Min /Wait "Files\exclamation01.vbs">nul 2>&1
    
    Set location-of-files > Files\Config.ini
    Set Serie_Anime >> Files\Config.ini
    Set Number > Files\command.ini
    
    If Exist "C:\Users\%username%\Desktop\%ep2% - %Serie_Anime%.lnk" Del /q "C:\Users\%username%\Desktop\%ep2% - %Serie_Anime%.lnk">nul 2>&1 & Start /Min /Wait Files\DesktopRefresh.exe>nul 2>&1
    
    setlocal EnableDelayedExpansion
    
    For /f "usebackq delims=" %%x in ("Files\command.ini") do (set "%%x")
    For /f "usebackq delims=" %%x in ("Files\Config.ini") do (set "%%x")
    
    set "cmd=findstr /R /N "^^" Files\Files.ini | find /C ":""
    
    for /f %%a in ('!cmd!') do set Numbers=%%a
    
    set lines=%Number%
    set Atual=1
    for /f "delims=" %%a in ('type Files\Files.ini') do (
        for %%b in (!lines!) do (
            if !Atual!==%%b Set "Ep1=%%a"
        )
        set /a "Atual = Atual + 1"
    )
    
    Set "Ep2=%Ep1%"
    set "find=*."
    call set delete=%%Ep2:!find!=%%
    call set Ep2=%%Ep2:!delete!=%%
    Set Ep2=%Ep2:.=%
    
    Set Ep1 > Files\command.ini
    Set Ep2 >> Files\command.ini
    Set lines >> Files\command.ini
    Set Number >> Files\command.ini
    
    endlocal
    
      

    提前致谢。

2 个答案:

答案 0 :(得分:2)

Set Arg = WScript.Arguments
set WshShell = createObject("Wscript.Shell")
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout
    Set rs = CreateObject("ADODB.Recordset")
    If LCase(Arg(1)) = "n" then
    With rs
        .Fields.Append "SortKey", 4 
        .Fields.Append "Txt", 201, 5000 
        .Open
        Do Until Inp.AtEndOfStream
            Lne = Inp.readline
            SortKey = Mid(Lne, LCase(Arg(3)), LCase(Arg(4)) - LCase(Arg(3)))
            If IsNumeric(Sortkey) = False then
                Set RE = new Regexp
                re.Pattern = "[^0-9\.,]"
                re.global = true
                re.ignorecase = true
                Sortkey = re.replace(Sortkey, "")
            End If
            If IsNumeric(Sortkey) = False then
                Sortkey = 0
            ElseIf Sortkey = "" then
                Sortkey = 0
            ElseIf IsNull(Sortkey) = true then
                Sortkey = 0
            End If
            .AddNew
            .Fields("SortKey").value = CSng(SortKey)
            .Fields("Txt").value = Lne
            .UpDate
        Loop
        If LCase(Arg(2)) = "a" then SortColumn = "SortKey ASC"
        If LCase(Arg(2)) = "d" then SortColumn = "SortKey DESC"
        .Sort = SortColumn
        Do While not .EOF
            Outp.writeline .Fields("Txt").Value
            .MoveNext
        Loop
    End With

    ElseIf LCase(Arg(1)) = "d" then
    With rs
        .Fields.Append "SortKey", 4 
        .Fields.Append "Txt", 201, 5000 
        .Open
        Do Until Inp.AtEndOfStream
            Lne = Inp.readline
            SortKey = Mid(Lne, LCase(Arg(3)), LCase(Arg(4)) - LCase(Arg(3)))
            If IsDate(Sortkey) = False then
                Set RE = new Regexp
                re.Pattern = "[^0-9\\\-:]"
                re.global = true
                re.ignorecase = true
                Sortkey = re.replace(Sortkey, "")
            End If
            If IsDate(Sortkey) = False then
                Sortkey = 0
            ElseIf Sortkey = "" then
                Sortkey = 0
            ElseIf IsNull(Sortkey) = true then
                Sortkey = 0
            End If
            .AddNew
            .Fields("SortKey").value = CDate(SortKey)
            .Fields("Txt").value = Lne
            .UpDate
        Loop
        If LCase(Arg(2)) = "a" then SortColumn = "SortKey ASC"
        If LCase(Arg(2)) = "d" then SortColumn = "SortKey DESC"
        .Sort = SortColumn
        Do While not .EOF
            Outp.writeline .Fields("Txt").Value
            .MoveNext
        Loop
    End With


    ElseIf LCase(Arg(1)) = "t" then
    With rs
        .Fields.Append "SortKey", 201, 260 
        .Fields.Append "Txt", 201, 5000 
        .Open
        Do Until Inp.AtEndOfStream
            Lne = Inp.readline
            SortKey = Mid(Lne, LCase(Arg(3)), LCase(Arg(4)) - LCase(Arg(3)))
            .AddNew
            .Fields("SortKey").value = SortKey
            .Fields("Txt").value = Lne
            .UpDate
        Loop
        If LCase(Arg(2)) = "a" then SortColumn = "SortKey ASC"
        If LCase(Arg(2)) = "d" then SortColumn = "SortKey DESC"
        .Sort = SortColumn
        Do While not .EOF
            Outp.writeline .Fields("Txt").Value
            .MoveNext
        Loop
    End With
    ElseIf LCase(Arg(1)) = "tt" then
    With rs
        .Fields.Append "SortKey", 201, 260 
        .Fields.Append "Txt", 201, 5000 
        .Open
        Do Until Inp.AtEndOfStream
            Lne = Inp.readline
            SortKey = Trim(Mid(Lne, LCase(Arg(3)), LCase(Arg(4)) - LCase(Arg(3))))
            .AddNew
            .Fields("SortKey").value = SortKey
            .Fields("Txt").value = Lne
            .UpDate
        Loop
        If LCase(Arg(2)) = "a" then SortColumn = "SortKey ASC"
        If LCase(Arg(2)) = "d" then SortColumn = "SortKey DESC"
        .Sort = SortColumn
        Do While not .EOF
            Outp.writeline .Fields("Txt").Value
            .MoveNext
        Loop
    End With
    End If

使用

cscript //nologo script.vbs sort {n|d|t|tt} {a|d} startcolumn  endcolumn < input.txt > output.txt

选项

n - extracts a number from the columns specified. Looks for the first number.
d - extracts a time or date from the columns specified. Looks for the first date.
t - extracts a text string including spaces from the columns specified.
tt - extracts a text string discarding leading and trailing spaces from the columns specified.
a - sorts acending
d - sorts decending
startcolumn - the starting column, the first character is column 1
endcolumn - the ending column

这就是synax命令行的含义

下表描述了用于指示命令行语法的表示法。

符号说明

Text without brackets or braces
 Items you must type as shown

<Text inside angle brackets>
 Placeholder for which you must supply a value

[Text inside square brackets]
 Optional items

{Text inside braces}
 Set of required items; choose one

Vertical bar (|)
 Separator for mutually exclusive items; choose one

Ellipsis (…)
 Items that can be repeated

答案 1 :(得分:0)

经过大量搜索,我找到了hybrid JScript/batch utility called REPL.BAT dbenham并得到了我想要的内容,根据我的代码编写了脚本:

Del /q "Files\Files.ini">nul 2>&1

for /f "tokens=2 delims=:" %%F in (
  'dir /b /a-d "%location-of-files%\*.*"^|Files\repl "^(\w+).*" "00000$1:$&" a^|Files\repl ".*(\d{5}:)" "$1"^|sort'
) do echo %%F >> Files\Files.ini