[英]batch file loop not showing most recent directory
我試圖遍歷一個目錄,然后顯示出子目錄(該子目錄可能埋在其他目錄中),並拉出最近創建的目錄。 我在這里有一個循環,它將進入已設置的根目錄,並僅查看目錄,合並所有子目錄,按創建日期對它們進行排序,然后將循環設置為列表末尾的最大最近,然后回顯。 但是,由於某些原因,排序無法正確進行。 它會不斷拉出不是最新目錄。 我似乎無法查明問題。 是什么原因造成的? 我使用的排序正確嗎? 它不會將子目錄與其他級別的其他目錄進行比較嗎? 任何幫助將不勝感激。
@ECHO OFF
SET dir=C:\Users\Darren\Google Drive\
echo %dir%
FOR /F "delims=" %%i IN ('dir "%dir%" /b /s /ad-h /t:c /od') DO (
echo %%i
SET a=%%i)
SET sub=%a%
echo Most recent subfolder: %sub%
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q43579378.txt"
SET "filename2=%sourcedir%\q43579378_2.txt"
(
FOR /f "delims=" %%a IN (
'dir /b /s /ad "%sourcedir%\*" '
) DO ECHO %%~a
)>"%filename1%"
IF EXIST "%filename2%" (
FOR /f "delims=" %%a IN ('findstr /L /V /i /x /g:"%filename2%" "%filename1%"') DO (
FOR /f %%c IN ('dir /b /a-d "%%a\*" 2^>nul^|find /c /v "" ') DO (
IF %%c gtr 20 ECHO(send email directory %%a has %%c files
)
)
)
MOVE "%filename1%" "%filename2%" >nul
GOTO :EOF
我使用了一個測試目錄作為sourcedir
。 自然,它不必與您要掃描的目錄相同,文件名可以是您想要的任何名稱。
第一個for
生成一個當前存在於filename1
中的絕對目錄filename1
的列表(使用for
一對“備用”括號括住for
允許重定向)
如果存在filename2
(將在第一次運行后的每次運行中使用),然后使用/L
文字查找filename1
和filename2
之間的差異, /v
與/i
不區分大小寫/x
精確匹配/g:
此字符串文件,並將每個新目錄名稱分配給%%a
。
然后,僅掃描"%%a"
目錄(僅文件),抑制file not found
消息並計算返回的行數( /c
計數/v
行不匹配""
為空字符串)。 將結果分配給%%c
,進行測試並選擇要采取的措施。
for ... %%c
命令中要執行的命令中的重定向器之前的插入符號^
逃避了重定向器,因此它們被解釋為要執行的命令的一部分,而不是for
。
dir /S /O
命令分別對每個子目錄的內容進行排序,並且無法更改該行為。
一種可能的替代方法是使用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%
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.