的dir /S /O
命令排序每個單獨的子目錄的內容,並沒有辦法改變這種行爲。
一種可能的替代方法是在標準化的,可排序的,區域 - 和locale-使用wmic
命令,這是有點慢,但能夠導出所述日期信息的(創建,最後修改,上次訪問)獨立格式:
wmic FSDir where Name="D:\\Data" get CreationDate /VALUE
wmic FSDir where (Name="D:\\Data") get CreationDate /VALUE
所以下面兩個腳本使用一個for /D /R
循環得到所有的(子)目錄,上面wmic
命令行,for /F
捕捉到它們的輸出和一個簡單的命令由做排序年齡。
第一個與他們的創建日期創建一個臨時文件,收集所有的目錄,同時,按以下格式:
20170424215505.000000+060 D:\Data
對於排序,使用sort
命令。下面是代碼:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=%~1" & rem // (use first command line argument as the root directory)
set "_PATTERN=*" & rem // (search pattern for directories; `*` matches all)
set "_TMPF=%TEMP%\%~n0_%RANDOM%.tmp" & rem // (specify a temporary file)
rem // Write list of directory paths preceded by their creation dates to temporary file:
> "%_TMPF%" (
set "ERR=0"
rem // Enumerate all matching directories recursively:
for /D /R "%_ROOT%" %%D in ("%_PATTERN%") do (
rem // Store currently iterated directory path:
set "DIRPATH=%%~D"
rem // Toggle delayed expansion to avoid trouble with the exclamation mark:
setlocal EnableDelayedExpansion
(
rem /* Capture `wmic` output to query creation date of currently iterated
rem directory in locale-dependent and sortable format: */
for /F "tokens=2 delims==" %%L in ('
rem/ Do two attempts, because one approach can handle `^)` and one can handle `,`; ^&^
rem/ note that `wmic` cannot handle paths containing both of these characters: ^&^
2^> nul wmic FSDir where Name^="!DIRPATH:\=\\!" get CreationDate /VALUE ^|^|^
2^> nul wmic FSDir where ^(Name^="!DIRPATH:\=\\!"^) get CreationDate /VALUE
') do (
rem // Do nested loop to avoid Unicode conversion artefacts (`wmic` output):
for /F %%K in ("%%L") do echo(%%K !DIRPATH!
)
) || (
rem // This is only executed in case a path contains both `)` and `,`:
>&2 echo ERROR: Could not handle directory "!DIRPATH!"^^!
set "ERR=1"
)
endlocal
)
)
rem /* Return content of temporary file in sorted manner using `sort` command,
rem remember last item of sorted list; clean up temporary file: */
for /F "tokens=1*" %%C in ('sort "%_TMPF%" ^& del "%_TMPF%"') do set "LASTDIR=%%D"
rem // Return newest directory:
echo "%LASTDIR%"
endlocal
exit /B %ERR%
第二個存儲的每個目錄在名爲創建日期的變量,在下面的格式:
$20170424215505.000000+060=D:\Data
對於排序,所述使用set
命令。下面是代碼:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=%~1" & rem // (use first command line argument as the root directory)
set "_PATTERN=*" & rem // (search pattern for directories; `*` matches all)
set "ERR=0"
rem // Clean up variables beginning with `$`:
for /F "delims==" %%C in ('2^> nul set "$"') do set "%%C="
rem // Enumerate all matching directories recursively:
for /D /R "%_ROOT%" %%D in ("%_PATTERN%") do (
rem // Store currently iterated directory path:
set "DIRPATH=%%~D"
rem // Toggle delayed expansion to avoid trouble with the exclamation mark:
setlocal EnableDelayedExpansion
(
rem /* Capture `wmic` output to query creation date of currently iterated
rem directory in locale-dependent and sortable format: */
for /F "tokens=2 delims==" %%L in ('
rem/ Do two attempts, because one approach can handle `^)` and one can handle `,`; ^&^
rem/ note that `wmic` cannot handle paths containing both of these characters: ^&^
2^> nul wmic FSDir where Name^="!DIRPATH:\=\\!" get CreationDate /VALUE ^|^|^
2^> nul wmic FSDir where ^(Name^="!DIRPATH:\=\\!"^) get CreationDate /VALUE
') do (
rem // Do nested loop to avoid Unicode conversion artefacts (`wmic` output):
for /F %%K in ("%%L") do (
rem /* Assign currently iterated path to variable named of the
rem respective creation date preceded by `$`: */
endlocal & set "$%%K=%%~D"
)
)
) || (
endlocal
rem // This is only executed in case a path contains both `)` and `,`:
>&2 echo ERROR: Could not handle directory "%%~D"!
set "ERR=1"
)
)
rem /* Return all variables beginning with `$` in sorted manner using `set` command,
rem remember last item of sorted list: */
for /F "tokens=1* delims==" %%C in ('2^> nul set "$"') do set "LASTDIR=%%D"
rem // Return newest directory:
echo "%LASTDIR%"
endlocal
exit /B %ERR%
'DIR/od'日期爲當前目錄的順序排序,所以你的代碼將選擇在最後一個目錄的最後創建的目錄名遇到過,不是最後創建-OF-所有的目錄和子目錄。如果需要最後創建的目錄名,則需要對提取的日期進行排序的目錄列表以yymmddhhmmss格式排序。不是特別難,但也不是一條線。考慮你想要創建日期還是最新更新日期 - 以及爲什麼你需要該目錄?你真的在尋找最後(創建)的文件嗎? – Magoo
這就是我的想法。我不能只建立一個所有目錄的列表,然後按創建的日期對它們進行排序嗎?我的最終目標是找出最近創建的目錄,並在其中包含超過20個文件時發送電子郵件通知。我有腳本的所有其他方面的工作,只是不排序。 –
如果他們都在同一個目錄中,是的。如果不是,則需要通過將日期字符串(需要知道使用哪種格式)分解爲yymm來比較日期...另一種方法是將目錄樹記錄到文件中,然後查看新的出現點 - 如果新的目錄不共享父母,你仍然需要通過比較日期來對它們進行排序。你不想被通知所有包含> 20個文件的新目錄嗎?這應該更容易建立。 – Magoo