cmd命令以数字顺序转储文件列表

时间:2016-10-17 02:12:04

标签: batch-file cmd

抱歉,我无法正确说出这一点,但我需要一个命令来创建一个文本文件,该文件以数字顺序转储文件名。这是执行我想要的命令但是在开始时有1个的所有命令都是成束的,2,3等......

(for %i in (*.flv) do @echo file '%i') > file.txt

2 个答案:

答案 0 :(得分:1)

例如,大多数cmd /批处理文件命令(例如dirsortset都会执行纯字母排序,因此不会特别处理任何数字部分。例如,string12出现在string3之前,因为1字符出现在2之前,并且使用了排序方式。要更改行为以便应用字母数字排序,这意味着string3出现在string12之前,您必须编写自己的代码才能完成此操作。通过将出现在字符串/文件名中的每个数字部分填充到固定的数字位数,以下脚本就是这样做的,在这种情况下,字母数字和字母排序顺序匹配。以下是代码,包括解释性说明。如您所见,完成任务需要复杂的代码,并使脚本对所有对cmd具有特殊含义的字符都是安全的(如 SPACE ,,{ {1}}。;以及=^&()%):

!

将(a)文件模式提供为(a)命令行参数,例如(假设批处理文件保存为@echo off setlocal EnableExtensions DisableDelayedExpansion rem // Define constants here: set "_TEMPFILE=%TEMP%\%~n0_%RANDOM%.tmp" & rem // (temporary file used for sorting) set /A "_DIGITS=12" & rem // (number of digits every numeric part is padded for sorting) rem // Build string containing enough zeros for padding: set "$PAD=" & setlocal EnableDelayedExpansion for /L %%D in (1,1,%_DIGITS%) do set "$PAD=!$PAD!0" endlocal & set "$PAD=%$PAD%" rem // Prepare temporary file containing data to sort: > "%_TEMPFILE%" ( rem // Gather and resolve all command line arguments: for %%I in (%*) do ( rem // Get pure file name: set "ITEM=%%~nxI" rem // Extend all numeric parts in file name to certain number of digits: call :PROCESS AUGM ITEM || >&2 echo ERROR: potential problem sorting "%%~nxI"! rem // Write extended and original file name into temporary file: setlocal EnableDelayedExpansion echo(!AUGM!^|!ITEM! endlocal ) ) rem // Return content of temporary file in ascendingly sorted order: for /F "tokens=2 delims=| eol=|" %%J in ('sort "%_TEMPFILE%"') do ( rem // Simply return each item: echo(%%J ) rem // Delete temporary file: del "%_TEMPFILE%" endlocal exit /B :PROCESS rtn_augmented_string ref_string rem /* Routine to augment a string so that every numeric part is padded with leading rem zeros to the left to hold a predefined number of digits: */ setlocal DisableDelayedExpansion set "#RTN=%~1" set "#ARG=%~2" rem // Initialise required variables: set "COLL=" set "ERRL=0" setlocal EnableDelayedExpansion for /F delims^=^ eol^= %%B in (^""!%#ARG%!"^") do ( endlocal set "PSTR=%%~B" setlocal EnableDelayedExpansion ) rem // Entry point for loop to handle one numeric string part: :REPEAT rem // Extract the string portions before and after the first numeric part: for /F "tokens=1,* delims=0123456789 eol=0" %%A in ("+!PSTR!") do ( endlocal set "PART=%%A" set "NEXT=%%B" rem // Determine length of string portion before first numeric part: call :LENGTH PLEN PART set /A "PLEN-=1" setlocal EnableDelayedExpansion set "PART=!PART:~1!" rem // Split off string portion before first numeric part from total string: for %%C in (!PLEN!) do ( if defined PSTR set "PSTR=!PSTR:~%%C!" ) rem /* Splitt off string portion after first numeric part from remaining string; rem this is nothing but extracting the first numeric part itself: */ call :SPLIT PNUM NEXT PSTR rem // Determine the actual length of the numeric part: call :LENGTH NLEN PNUM rem // Do the actual padding with leading zeros of the numeric part: if defined PNUM ( set "PNUM=%$PAD%!PNUM!" set "PNUM=!PNUM:~-%_DIGITS%!" ) rem // Store the part after the first numeric part: for /F delims^=^ eol^= %%C in (^""!NEXT!"^") do ( rem /* Build string with the string portion before the current numeric part rem and the padded current numeric part itself: */ for /F delims^=^ eol^= %%D in (^""!COLL!!PART!!PNUM!"^") do ( rem // Check whether the predefined number of padding digits is sufficient: for /F %%E in ("!NLEN!") do ( endlocal set "PSTR=%%~C" set "COLL=%%~D" if %%E GTR %_DIGITS% set "ERRL=1" setlocal EnableDelayedExpansion ) ) ) ) rem // Repeat the whole approach while there is still a remaining string portion: if defined PSTR goto :REPEAT rem // Return the string with padded numeric parts: for /F delims^=^ eol^= %%R in (^""!COLL!"^") do ( endlocal endlocal set "%#RTN%=%%~R" exit /B %ERRL% ) exit /B :SPLIT rtn_left_string ref_split_char val_string rem /* Routine to split a string at the first occurrence of a certain character and to rem return the portion before it: */ setlocal DisableDelayedExpansion set "#RTN=%~1" set "#CHR=%~2" set "#ARG=%~3" rem // Initialise required variables: setlocal EnableDelayedExpansion set "CHAR= " & if defined %#CHR% set "CHAR=!%#CHR%:~,1!" if "!CHAR!"=="<" (set "PREF=>") else (set "PREF=<") rem // Check whether a split character is defined: if defined %#CHR% ( rem /* Split character available, so split off first occurrence and everything after rem from the original string: */ for /F eol^=^%CHAR%^ delims^=^%CHAR% %%C in ("%PREF%!%#ARG%!") do ( endlocal set "%#RTN%=%%C" setlocal EnableDelayedExpansion set "%#RTN%=!%#RTN%:~1!" ) ) else ( rem // No split character defined, so do not split off anything: set "%#RTN%=!%#ARG%!" ) rem // Return the resulting string: for /F delims^=^ eol^= %%R in (^""!%#RTN%!"^") do ( endlocal endlocal set "%#RTN%=%%~R" ) exit /B :LENGTH rtn_length ref_string rem /* Routine to determine the length of a given string: */ setlocal DisableDelayedExpansion set "#RTN=%~1" set "#ARG=%~2" setlocal EnableDelayedExpansion rem // Check whether a string is provided: if defined %#ARG% ( rem // String is available, so calculate its length: set /A "%#RTN%=1" for %%A in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do ( if not "!%#ARG%:~%%A!"=="" ( set /A "%#RTN%+=%%A" set "%#ARG%=!%#ARG%:~%%A!" ) ) ) else ( rem // String is empty, so length is zero: set /A "%#RTN%=0" ) rem // Return the computed length: for /F %%R in ("!%#RTN%!") do ( endlocal endlocal set "%#RTN%=%%R" ) exit /B ):

sort-alpha-num.bat

要将生成的排序列表存储在名为sort-alpha-num.bat "*.flv" 的文本文件中,请使用:

file.txt

如果出现sort-alpha-num.bat "*.flv" > "file.txt" 之类的错误消息,请增加脚本顶部的位数(请参阅第ERROR: potential problem sorting "1000000000000.flv"!行)。

答案 1 :(得分:0)

既然您没有提供有关输出文件应该如何的更多信息吗?

@echo off
set "folder=%userprofile%\Desktop\*.flv"
set "listfiles=listfiles.txt"
If Exist %listfiles% Del %listfiles%
setLocal EnableDelayedExpansion
Rem Populate the array with existent files in this folder
for /f "tokens=* delims= " %%a in ('Dir /s /b /a:-d "%folder%"') do (
    set /a Count+=1
    set "File[!Count!]=%%~na"
)
::******************************************************************
:Display_Files
cls & color 0B
echo(
For /L %%i in (1,1,%Count%) do (
    echo %%i - !File[%%i]!
)
echo(
(
rem to save result into logfile
    For /L %%i in (1,1,%Count%) do (
    echo %%i - !File[%%i]!
    )
)>> %listfiles%
echo Hit any key to open %listfiles% :
Pause>nul & Start "" %listfiles%