批处理文件 - 将文件列表写入变量 [英] Batch file - Write list of files to variable
问题描述
我想将所有文件(包括它们的绝对路径)的列表放入一个变量中,用空格分隔.我的 Google-fu 在这方面似乎很弱,因为我一直遇到问题.
I'd like to get a list of all files (including their absolute path) into a variable, separated by spaces. My Google-fu seems to be weak in this regard, because I keep running into issues.
我有一个存储在 %baseDir%
中的基本目录,我想解析它的文件(不递归或包含子目录).就像我提到的,这需要进入一个列表.我想我可以使用一个不错的小快捷方式,但是 for 循环和连接也可以做到这一点.
I have a base directory stored in %baseDir%
, and would like to parse it for files (not recursing or including sub-directories). Like I mentioned, this needs to go into a list. I'd imagine there's a nice little shortcut I could use, but a for loop and concatenation would do the trick as well.
理想情况下,我会有这样的东西:
Ideally, I'd have something like this:
echo fileList
C:file1.c C:file2.c C:file3.c
推荐答案
很简单:
setlocal enabledelayedexpansion enableextensions
set LIST=
for %%x in (%baseDir%*) do set LIST=!LIST! %%x
set LIST=%LIST:~1%
事实上,您也可以在 set
命令的帮助中找到这个非常示例,可通过 help set
访问,并附有解释为什么天真的方法不会工作.
In fact, you find this very example also in the help for the set
command, accessible via help set
, complete with an explanation why the naïve approach won't work.
要使用不同的文件集(而不是所有),您可以轻松更改通配符:
To use a different set of files (rather than all), you can easily change the wildcard:
for %%x in (%baseDir%*.c) do set LIST=!LIST! %%x
这篇关于批处理文件 - 将文件列表写入变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!