正如 Q-title 中所說,我正在嘗試找到一個名為 的特定目錄Local State,但它可能被某些應用程式拼寫為LocalStateor Local State,其中任何一個肯定存在于里面的每個應用程式的檔案夾中%USERPROFILE%,我正在嘗試列出。
現在為此我必須寫兩行,其中一行是為了找出LocalState哪個行得通,它是給定的:
pushd "%USERPROFILE%"
for /d /r %%h in (LocalState) do if exist "%%h" echo "%%h"
popd
但是當我嘗試查找Local State檔案夾時使用幾乎相同的行,它沒有按預期顯示路徑,因為它在搜索的檔案夾周圍添加了額外的引號。看到這個:
pushd "%USERPROFILE%"
for /d /r %%h in ("Local State") do if exist "%%h" echo "%%h"
popd
給出了這個,這很奇怪,因為不能對這個額外的參考路徑采取任何行動:
....
....
"C:\Users\<Username>\AppData\Local\BraveSoftware\Brave-Browser\User Data\"Local State""
"C:\Users\<Username>\AppData\Local\Google\Chrome\User Data\"Local State""
....
....
現在我想知道是否有可能只有一行我能夠使用批處理腳本搜索檔案夾名稱LocalState或Local State在指定檔案夾中?像這樣的東西?
for /d /r %%h in ("Local? State") do if exist "%%h" echo "%%h"
它會以常規的正確參考格式顯示路徑,例如:?
....
....
"C:\Users\<Username>\AppData\Local\BraveSoftware\Brave-Browser\User Data\Local State"
"C:\Users\<Username>\AppData\Local\Google\Chrome\User Data\Local State"
....
....
或者,如果這根本不可能,那么我怎樣才能找到帶有空格的檔案夾名稱并以正確的引號格式回顯這些路徑,而不需要額外的、不需要的引號?
uj5u.com熱心網友回復:
為什么 FOR 命令列不能按預期作業?
字串 LocalState和"Local State"不被解釋for為要搜索的檔案夾名稱,因為既不包含也不*包含?。命令FOR僅搜索非隱藏檔案或/D在指定通配符模式時搜索非隱藏檔案夾的選項。
有試過:
for /d /r %%h in (LocalState) do if exist "%%h" echo "%%h"
for /d /r %%h in ("Local State") do if exist "%%h" echo "%%h"
上面的命令列導致遞回搜索目錄(包括隱藏目錄)并將每個找到的目錄分配給回圈變數,其中h每個目錄的完整路徑未包含在與指定" 字串或. LocalState"Local State"
示例:當前目錄C:\Temp具有以下目錄結構:
- C:\溫度
- 開發和測驗(!)
- 檔案夾 2
IF條件使用分配給回圈變數的以下字串執行h:
C:\Temp\LocalStateC:\Temp\Development & Test(!)\LocalStateC:\Temp\Folder 2\LocalStateC:\Temp\"Local State"C:\Temp\Development & Test(!)\"Local State"C:\Temp\Folder 2\"Local State"
目錄名稱 4 到 6 是有問題的,因為它們本身包含兩個雙引號,導致執行IF條件時指定的檔案系統條目名稱不正確 - 目錄或檔案或重新分析點 -在這種情況下,對于IF沒有任何區別最后的反斜杠。
有人可能認為FOR的這種行為沒有意義,但這種行為在某些用例中很有用,例如在目錄樹的每個檔案夾中創建具有特定名稱的檔案時。
這里的問題是不能*在開頭或結尾添加,即使用*LocalState或"Local State*"因此可能導致誤報。FORLocalState現在真的會搜索名稱以 . 結尾或以 . 開頭的非隱藏目錄Local State。
所以使用下面的命令列就不太好了:
for /d /r %%h in (*LocalState "Local State*") do echo "%%h"
有哪些可能的解決方案?
一個非常快速的可能解決方案是:
for /F "delims=" %%h in ('dir "%USERPROFILE%\LocalState" "%USERPROFILE%\Local State" /AD /B /S 2^>nul') do echo "%%h"
在后臺啟動了另外一個cmd.exe選項/c和附加的指定命令列'作為附加引數。
DIR先搜索
- 由于選項,僅用于目錄
/AD - 用名字
LocalState或名字Local State - 在指定的目錄
%USERPROFILE%和 - all its subdirectories because of option
/Sand - outputs just the fully qualified directory name because of the options
/B(bare format) and/S.
DIR is so smart to search in each directory for both directories names. So the entire directory tree is searched by DIR only once for both directory names at the same time.
The started cmd.exe closes itself once DIR finished.
The cmd.exe instance processing the batch file captures all fully qualified folder names output by DIR and FOR processes them now line by line.
The FOR option delims= defines an empty list of delimiters to turn off the default line splitting behavior on normal spaces and horizontal tabs. That is required because of each folder name should be assigned completely one after the other to the loop variable h for further processing and not just the part up to first space character in a full folder name.
Other solutions are:
for /F "delims=" %%h in ('dir "%USERPROFILE%\Local*State" /AD /B /S 2^>nul ^| %SystemRoot%\System32\findstr.exe /E /I /L /C:LocalState /C:"Local State"') do echo "%%h"
for /F "delims=" %%h in ('dir "%USERPROFILE%\Local*State" /AD /B /S 2^>nul ^| %SystemRoot%\System32\findstr.exe /E /I /R /C:"Local *State"') do echo "%%h"
DIR searches in both cases for directories of which name starts with Local and ends with State (case-insensitive) recursively in specified folder %USERPROFILE%.
There is used FINDSTR on the first command line to filter out all false positive found directories of which fully qualified directory name does not end with the case-insensitive and literally interpreted string LocalState or Local State like Local & State.
There is used FINDSTR on the second command line to filter out all false positive found directories of which fully qualified directory name is at end not matched by the case-insensitive interpreted regular expression Local *State which matches LocalState and Local State and also Local State (two spaces) because of * is interpreted here as preceding character (the space) zero or more times. Please notice the difference. In a wildcard pattern * means any character zero or more times, but not here in the regular expression search string interpreted by FINDSTR where it means preceding character zero or more times.
The two solutions searching with DIR for the directories with a wildcard pattern and using FINDSTR to filter out false positive found directories are a bit slower than the solution using just DIR with the two directory names to search for.
In all provided solutions could be modified the DIR option /AD to /AD-L to ignore junctions and symbolic directory links (reparse points) and find just real directories.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?echo /?findstr /?for /?
閱讀有關使用命令重定向運算子2>nul的 Microsoft 檔案,了解和的說明|。當 Windows 命令解釋器在執行命令FOR之前在后臺啟動的單獨命令列程中執行嵌入式命令列時,重定向運算子>和|必須^在FOR命令列上用脫字符號轉義以解釋為文字字符。
uj5u.com熱心網友回復:
這將batch-file在cmd.windows
FOR /F "delims=" %%A IN ('powershell -NoLogo -NoProfile -Command ^
"(Get-ChildItem -Recurse -Directory -Filter 'Local*State').FullName"') DO (ECHO Directory name is %%~A)
需要 PowerShell 5.1 或更高版本。使用命令
$PSVersionTable.PSVersion.ToString()或查找您的版本(Get-Host).Version.ToString()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/452858.html
