簡體   English   中英

批處理 - 將DIR的輸出寫入變量

[英]Batch - Write output of DIR to a variable

我必須將DIR的輸出存儲在一個變量中。

這是如之前詢問這里這里這里

它們都提供了與我現在使用的或多或少類似的答案:

%= Find the *.sln file and write its path to the file temp.temp =%
DIR /s/b *.sln > temp.temp

%= Read out the content of temp.temp again to a variable =%
SET /p texte=< temp.temp

%= Remove the temp.temp file =%
DEL temp.temp

%= Get only the filename without path =%
FOR %%A IN ("%texte%") DO (
    SET Name=%%~nxA
)

但在我的情況下,我敢肯定的輸出中DIR /s/b *.sln將是百達一襯墊 對我來說,它看起來有點難看
a)將輸出存儲在外部文件中
b)在它上面運行FOR循環雖然我已經知道它只有一行。

有沒有直接/簡單的方法來做到這一點?

for /f "delims=" %%a in ('dir /s /b *.sln') do set "name=%%a"

確實是最有效的方法(您可以直接處理命令的輸出,不需要臨時文件)。
%name%將包含文件的完整限定文件名(如果有多個文件,則包含最后一個文件)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM