以数字顺序循环浏览文件(类似于Windows资源管理器) [英] Loop through files in a numeric order (Like on Windows Explorer)

查看:126
本文介绍了以数字顺序循环浏览文件(类似于Windows资源管理器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组文件,希望通过FOR命令按数字进行排序,如下所示:

I have a group of files that I want to be sorted numerically with FOR command like this:

FOR %%G IN (*.pdf) DO (
ECHO %%G
)

有什么办法可以得到:

1.pdf
2.pdf
3.pdf
...
10.pdf
11.pdf

而不是:

1.pdf
10.pdf
11.pdf
12.pdf
...
2.pdf
21.pdf

就像在Windows资源管理器上订购它们一样? 我在Windows资源管理器中订购的文件

Like they are ordered on the Windows Explorer? My files ordered on Windows Explorer

推荐答案

这使用API​​调用StrCmpLogicalW

This implements shellsort using API call StrCmpLogicalW

StrCmpLogicalW将字符串中的数字视为数字.

StrCmpLogicalW treats numbers in strings as if they were numbers.

要使用

logicalsort < "C:\Windows\System32\NetSetupMig.log"

要进行编译,请在与vb文件相同的目录中运行此批处理文件.

To compile run this batch file in same directory as vb file.

REM LogicalSort.bat
REM This file compiles LogicalSort.vb to LogicalSort.exe
REM LogicalSort.exe sorts lines from stdin and writes to stdout. Numbers in strings are sorted as numbers.
REM To use 
REM LogicalSort < Inputfile.txt > Outputfile.txt
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\LogicalSort.exe" "%~dp0\LogicalSort.vb"
pause


'LogicalSort.vb
imports System.Runtime.InteropServices 
Public Module MyApplication  

Public Declare UNICODE Function StrCmpLogicalW Lib "Shlwapi" (ByVal FirstStr as String, ByVal SecondStr As String) as Integer

Sub Main
    Dim MyArray(15) as String
    Dim TempStr As String
    Dim Line As Object
    TempStr = ""
    Line=Console.readline
    Do Until Line = Nothing
        TempStr = TempStr & vbcrlf & Line
        Line=Console.readline
    Loop
    MyArray = Split(TempStr, vbcrlf)

    Sort(MyArray)
End Sub

Sub Sort(MyArray() As String)
    Dim TempVal As String
    Dim i As Integer
    Dim GapSize As Integer
    Dim CurPos As Integer
    Dim FirstLine As Integer
    Dim LastLine As Integer
    Dim NumLines As Integer
    FirstLine = LBound(MyArray)
    LastLine = UBound(MyArray)
    NumLines = LastLine - FirstLine + 1
    Do
        GapSize = GapSize * 3 + 1
    Loop Until GapSize > NumLines
    Do
        GapSize = GapSize \ 3
        For i = (GapSize + FirstLine) To LastLine
            CurPos = i
            TempVal = MyArray(i)
            Do While CompareResult( MyArray(CurPos - GapSize),TempVal)
                MyArray(CurPos) = MyArray(CurPos - GapSize)
                CurPos = CurPos - GapSize
                If (CurPos - GapSize) < FirstLine Then Exit Do
            Loop
            MyArray(CurPos) = TempVal
        Next
    Loop Until GapSize = 1

    For each thing in MyArray
        Console.writeline(thing)
    Next
 End Sub

Private Function CompareResult(Value1 As String, Value2 As String) as Boolean
    CompareResult = (StrCmpLogicalW(Value1, Value2) = 1)
End Function


End Module 

这篇关于以数字顺序循环浏览文件(类似于Windows资源管理器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆