幾天來,我一直在嘗試使用批處理腳本解決以下問題,但沒有成功:
我需要位于檔案夾中的檔案的完整路徑:/common/Valheim/valheim.exe
應在所有驅動器中搜索此路徑,最后應將路徑保存在不帶檔案名的變數中。
這會找到所有 valheim.exe 檔案,但如果我添加檔案夾,我沒有得到任何結果。
setlocal enabledelayedexpansion
@echo off
(
for %%a in ( c d e f g h) do (
if exist "%%a:\" dir "%%a:\valheim.exe" /b /s /a-d (
@set _variable=%%a
@echo !_variable!
)
)
)
endlocal
例如,我如何在函式外部使用變數來復制檔案。
uj5u.com熱心網友回復:
由于多種原因,問題中的代碼不起作用:
if exist "%%a:\"只是測驗驅動器的存在,或者更準確地說,測驗當前分配給回圈變數的驅動器號的驅動器的根目錄a。- 如果驅動器存在,則執行命令DIR以在整個驅動器中搜索名稱為 的檔案
valheim.exe。但是命令DIR的結果根本不被評估。DIR僅輸出所有找到的名稱為完整路徑的檔案,或者在無法在整個驅動器上找到名稱為檔案valheim.exe的錯誤訊息。既沒有評估命令DIR的退出代碼,也沒有評估DIR的輸出。File Not Foundvalheim.exe - 對于分配給回圈變數的每個驅動器號執行以開頭、具有兩個命令
(行@set _variable=%%a并@echo !_variable!以. 僅當IF條件為真且DIR找到名稱為 的檔案時,在行尾用IF條件標記命令塊的開頭不會導致運行此命令塊。IF的語法)a(valheim.exe這里的條件是完全錯誤的,使命令塊中的兩個命令的執行依賴于IF條件。 - 出于這個原因,環境變數
_variable總是用FOR回圈完成h后的值定義。 _variable具有值的環境變數h被命令丟棄,endlocal這會導致丟棄整個環境變數串列,該串列在通過命令啟動批處理檔案時創建為當前環境變數串列的副本,setlocal只修改環境變數的(重新)定義在FOR回圈中_variable多次最終獲得字串值。因此,在根本沒有在啟動批處理檔案時定義的命令之后不再定義環境變數,或者仍然使用該環境變數在啟動批處理檔案時具有的值定義。h_variableendlocal- DIR不支持搜索具有特定路徑的檔案系統條目,該路徑位于檔案/檔案夾名稱的開頭并帶有可變路徑部分。沒有 Windows 命令支持它。只能搜索具有特定名稱的檔案或檔案夾并查看其完全限定名稱以驗證檔案/檔案夾是否位于特定目錄樹中。
該任務可以使用以下單個命令列通過批處理檔案完成:
@for /F "skip=1" %%I in ('%SystemRoot%\System32\wbem\wmic.exe LOGICALDISK GET DeviceID 2^>nul') do @for /F "delims=" %%J in ('dir %%I\valheim.exe /A-D /B /S 2^>nul ^| %SystemRoot%\System32\findstr.exe /E /I /L /C:"\\common\\Valheim\\valheim.exe"') do @set "FilePath=%%~dpJ" & goto :EOF
@left to a command 會在執行前由 Windows 命令處理器決議后抑制命令列的輸出。@如果批處理檔案包含的不僅僅是這一行并且作為第一行,則可以洗掉這三個@echo off。
首先在后臺使用命令列再執行一個 Windows 命令處理器:
C:\Windows\System32\cmd.exe /c C:\Windows\System32\wbem\wmic.exe LOGICALDISK GET DeviceID 2>nul
Windows Management Instrumentation 命令列實用程式使用Win32_LogicalDisk 類獲取有關邏輯驅動器和 Unicode 輸出的資訊,字符編碼為 UTF-16 LE 和 BOM 驅動器字母串列,帶有冒號和標題行DeviceID。
后臺命令列程的wmic.exe標準輸出流的輸出由處理批處理檔案的命令處理器捕獲。捕獲的行在后臺啟動后由FOR逐行處理,在終止cmd.exe后自行關閉。wmic.exe
DeviceID由于使用了選項,因此跳過了第一行skip=1。其他行僅包含驅動器號和驅動器的冒號。因此,外部FOR將一個驅動器號 冒號分配給回圈變數I并運行內部FOR。
內部FOR在后臺再次啟動cmd.exe,命令列如下:
C:\Windows\System32\cmd.exe /c dir C:\valheim.exe /A-D /B /S 2>nul | C:\Windows\System32\findstr.exe /E /I /L /C:"\\common\\Valheim\\valheim.exe"
搜索的內部命令DIRcmd.exe
- 僅用于檔案,因為選項
/A-D(屬性不是目錄) - 由于選項,名稱
valheim.exe在C:\及其所有子目錄中/S - 并由于 option 以裸格式輸出具有該名稱的所有檔案,
/B這意味著由于 option 僅具有具有完整路徑的檔案名/S。
通過將 DIR 重定向到設備NUL來抑制DIR輸出的錯誤訊息,即找不到任何名稱為句柄標準錯誤的檔案。valheim.exe
DIR輸出的檔案名被重定向到用作過濾器的FINDSTR的標準輸入。它對雙引號后指定/I的字面解釋 (/L和/C:) 搜索字串執行不區分大小寫的搜索 ( )。/C:僅當在行尾找到所搜索的字串 ( ) 時,查找才是肯定的,/E在這種情況下,整行由FINDSTR輸出到后臺命令列程的標準輸出。
The directory separator is \ on Windows and not / as on Linux/Mac. That is described by the Microsoft documentation about Naming Files, Paths, and Namespaces. FINDSTR interprets a backslash as escape character even in literally interpreted strings and for that reason it is necessary to escape each backslash in search string with one more backslash.
Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul and |. The redirection operators > and | must be escaped with caret character ^ on FOR command line to be interpreted as literal characters when Windows command interpreter processes this command line before executing the first command FOR.
The output by DIR filtered with FINDSTR is captured by cmd.exe processing the batch file and is processed line by line by the inner FOR after started background command process closed itself.
There is assigned just the full path of found file name of which fully qualified file name ends case-insensitive with \common\Valheim\valheim.exe to the environment variable FilePath and next the two loops and the entire batch file execution is exiting by using goto :EOF. There could be also used exit /B which is exactly the same. If the batch file should do more with usage of FilePath, then the command GOTO must be used with a label which has the next line in the batch file to just exit the two loops and continue batch file processing on the line below the label line.
The environment variable FilePath can be used further in the Windows command process used to process the batch file.
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 /?goto /?set /?wmic /?wmic logicaldisk /?wmic logicaldisk get /?
Read also:
- Where does GOTO :EOF return to?
- Single line with multiple commands using Windows batch file
- How to pass environment variables as parameters by reference to another batch file?
It explains very detailed what happens on each execution of the commands SETLOCAL and ENDLOCAL and why both are not used here in this batch file because of the environment variableFilePathshould still be defined with the found path after finishing processing of the batch file. - What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?
It explains in full details on the example of most important environment variablePATHhow environment variables are managed on Windows. The environment variableFilePathexists only forcmd.exeprocessing the batch file once defined by the batch file. It does not exist for other processes already running or started next by another process thancmd.exeprocessing the batch file. - Microsoft documentation about Application Registration
Many installers of applications register the executable as recommended by Microsoft. For that reason it is not necessary to search on all drives in all directories for an executable which is registered by the installer. A simple registry query is in this case enough to find out if an executable is installed at all and where it is installed. If the application registration is not done by an installer as recommended by Microsoft which is typical for games, the installers usually save the installation path either somewhere in the Windows registry or in a file in one of the common folders accessible using the predefined Windows Environment Variables output with their values on runningsetin a command prompt window.
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/428992.html
標籤:批处理文件
上一篇:從.txt串列批量創建符號鏈接
下一篇:在批處理檔案中生成程式命令
