我需要将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
的输出始终是单行的。对我来说,将输出存储在外部文件中并运行一个FOR
循环虽然已经知道它只有一行,看起来有点丑陋。
有没有更简单直接的方法来做到这一点?
delims
是我称之为texte
的变量,对吗?我猜我仍然可以使用name=%%~nxA
来仅获取文件名? - derHugodelims=
sets delims to "no delimiter". By default,for /f
is processed withtokens=1
and delimiters of space, comma, tab. If you leave it to standards, it will give you the very first word of the output only: Try (directly on command line)for /f %a in ("hello world") do @echo %a
vsfor /f "delims=" %a in ("hello world") do @echo %a
- Stephan%%a
或for
删除感叹号。这是延迟执行。在此之前关闭它 (setlocal disabledelayedexpansion
)。 - Stephan