Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
646 views
in Technique[技术] by (71.8m points)

vbscript - Sorting files by numerical order

I have a batch that create shortcut based on the order of files, the problem is that when it comes to numbers he presents the following problem when passing the number 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

I searched here and found various methods however the script I use works with folders and files that sometimes use accents, & and/or !

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

I wonder if there is any way to check the content and organize it can be properly before you save it in .ini or after saving the same in .ini.

Observations:

  1. The folder path is loaded the first time the Set command.
  2. After entering the path he saved in an .ini file and always loaded.
  3. The Script list only files within the directory does not list subfolders and files and folders within it.
  4. The script needs other files to work the download link is below: https://www.mediafire.com/?zcoybkfo8k4nm1t

My Full Code:

@Echo off

Title Create shortcuts in alphabetical order
mode con:lines=3 cols=25
Color 1f

CD /D "%~dp0"

If Exist "Filescommand.ini" For /f "usebackq delims=" %%x in ("Filescommand.ini") do (set "%%x")
If Exist "FilesConfig.ini" For /f "usebackq delims=" %%x in ("FilesConfig.ini") do (set "%%x")
If Exist "FilesFiles.ini" Goto shortcuts
If Exist "Filescommand.ini" Goto shortcuts
If Exist "FilesConfig.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 FilesError.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 "FilesConfig.ini" For /f "usebackq delims=" %%x in ("FilesConfig.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 FilesDesktopRefresh.exe>nul 2>&1 & Goto end
Dir /a-d /b "%location-of-files%" >FilesFiles.ini
Echo r1u4unoiwqa6.ending >>FilesFiles.ini

Start "exclamation01" /Min /Wait "Filesexclamation01.vbs">nul 2>&1

Set location-of-files > FilesConfig.ini
Set Serie_Anime >> FilesConfig.ini
Set Number > Filescommand.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 FilesDesktopRefresh.exe>nul 2>&1

setlocal EnableDelayedExpansion

For /f "usebackq delims=" %%x in ("Filescommand.ini") do (set "%%x")
For /f "usebackq delims=" %%x in ("FilesConfig.ini") do (set "%%x")

set "cmd=findstr /R /N "^^" FilesFiles.ini | find /C ":""

for /f %%a in ('!cmd!') do set Numbers=%%a

set lines=%Number%
set Atual=1
for /f "delims=" %%a in ('type FilesFiles.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 > Filescommand.ini
Set Ep2 >> Filescommand.ini
Set lines >> Filescommand.ini
Set Number >> Filescommand.ini

endlocal

Start "exclamation02" /Min /Wait "Filesexclamation02.vbs">nul 2>&1

For /f "usebackq delims=" %%x in ("Filescommand.ini") do (set "%%x")
For /f "usebackq delims=" %%x in ("FilesConfig.ini") do (set "%%x")

IF "%Ep2%"=="r1u4unoiwqa6" Goto end

Start /Min /Wait FilesShortcut.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%>Filesshortcut.ini & Start /Min /Wait Filesshortcut.vbs>nul 2>&1
Set /A Number = %lines% + 1
:::::::::::::::::::::::::::::::::::::::::::::
Set location-of-files > FilesConfig.ini
Set Serie_Anime >> FilesConfig.ini
:::::::::::::::::::::::::::::::::::::::::::::
Set Ep1 > Filescommand.ini
Set Ep2 >> Filescommand.ini
Set Number >> Filescommand.ini
:::::::::::::::::::::::::::::::::::::::::::::
Exit

:end
If Not Exist "%location-of-files%" Start /Wait FilesPDoM.vbs>nul 2>&1
If Exist "%location-of-files%" Start /Wait Filesending.vbs>nul 2>&1
If Exist "%location-of-files%" Start "Anime" "%location-of-files%">nul 2>&1
Del /q "FilesFiles.ini">nul 2>&1
Del /q "Filesshortcut.ini">nul 2>&1
Del /q "Filescommand.ini">nul 2>&1
Del /q "FilesConfig.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

Part where you need to use the DIR:

:shortcuts
If Exist "FilesConfig.ini" For /f "usebackq delims=" %%x in ("FilesConfig.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 FilesDesktopRefresh.exe>nul 2>&1 & Goto end
Dir /a-d /b "%location-of-files%" >FilesFiles.ini
Echo r1u4unoiwqa6.ending >>FilesFiles.ini

Start "exclamation01" /Min /Wait "Filesexclamation01.vbs">nul 2>&1

Set location-of-files > FilesConfig.ini
Set Serie_Anime >> FilesConfig.ini
Set Number > Filescommand.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 FilesDesktopRefresh.exe>nul 2>&1

setlocal EnableDelayedExpansion

For /f "usebackq delims=" %%x in ("Filescommand.ini") do (set "%%x")
For /f "usebackq delims=" %%x in ("FilesConfig.ini") do (set "%%x")

set "cmd=findstr /R /N "^^" FilesFiles.ini | find /C ":""

for /f %%a in ('!cmd!') do set Numbers=%%a

set lines=%Number%
set Atual=1
for /f "delims=" %%a in ('type FilesFiles.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 > Filescommand.ini
Set Ep2 >> Filescommand.ini
Set lines >> Filescommand.ini
Set Number >> Filescommand.ini

endlocal

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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

To use

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

Options

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

This is what command line synax means

The following table describes the notation used to indicate command-line syntax.

Notation Description

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...