まず/a-d
、 と/od
は のスイッチではありませんdir
。私はあなたが意味/a:d
したと信じています/o:d
。
これらのエラーとは別に、最初のfor
ステートメントが機能しない理由は、ファイルではなく*と呼ばれるディレクトリDIR \stDesign.cmd /B /S /a:d
を検索しているためです。stDesign.cmd
2 番目のfor
ステートメントはDIR
、ファイルを作成日順に並べるように指示していますが、これは同じディレクトリ内のファイルに対してのみ機能し、ディレクトリ間では機能しません。つまり、同じディレクトリ内の一致するファイルは日付順に並べ替えられますが、別のディレクトリ内に存在するファイルは、DIR
それらを見つけた順序になります。
試す
@echo off
echo.
REM Housekeeping: Delete any existing files left-over from previous runs
if exist "%tmp%\~.tm?" del "%tmp%\~.tm?"
REM Find the files
for %%v in (*.bat) do (
REM Extract Date, Time, Path, File Name, and Extention
for /f "tokens=1-3* delims=/ " %%w in ("%%~tdpnxv") do (
REM Change the date around so that simply sorting the lines
REM will order them by date and time.
echo %%y/%%w/%%x %%z>>%tmp%\~.tmp
)
)
REM Sort (in ascending order) and Display data
type "%tmp%\~.tmp" | sort
REM Housekeeping
del "%tmp%\~.tmp"
echo.
REM または、時刻と日付を表示したくない場合は、type 行を次のように置き換えます。
@echo off
echo.
REM Housekeeping: Delete any existing files left-over from previous runs
if exist "%tmp%\~.tm?" del "%tmp%\~.tm?"
REM Find the files
for %%v in (*.bat) do (
REM Extract Date, Time, Path, File Name, and Extention
for /f "tokens=1-3* delims=/ " %%w in ("%%~tdpnxv") do (
REM Change the date around so that simply sorting the lines
REM will order them by date and time.
echo %%y/%%w/%%x %%z>>%tmp%\~.tmp
)
)
REM Sort (in ascending order) and save data to ~.tm2
type "%tmp%\~.tmp" | sort >> "%tmp%\~.tm2"
REM Extract path and file name only from ~.tm2 and display it
for /f "tokens=4* usebackq" %%x in ("%tmp%\~.tm2") do echo %%x
REM Housekeeping
del "%tmp%\~.tmp"
echo.
最初の例では、次のような出力が得られます。
1992\01\15 00:12:55 C:\SOMEPATH\FILE0001.EXT
2004\04\17 00:42:17 C:\ANOTHERPATH\FILE0002.EXT
2004\04\17 12:42:17 C:\MISCPATH\RANDOM\FILE0003.EXT
2012\10\15 00:12:55 C:\EXAMPLE\FILE0004.EXT
2012\01\15 00:12:55 C:\FILE0005.EXT
2 番目の例では、パスとファイル名のみが表示されますが、日付と時刻の順に並べられています。
C:\SOMEPATH\FILE0001.EXT
C:\ANOTHERPATH\FILE0002.EXT
C:\MISCPATH\RANDOM\FILE0003.EXT
C:\EXAMPLE\FILE0004.EXT
C:\FILE0005.EXT
並べ替え順序を逆にするには、次のように /R スイッチを と共に使用しますsort
。
type tmp.txt | sort /R
※ディレクトリとフォルダは同じものです。