批处理文件 - 文件写入变量列表 [英] Batch file - Write list of files to variable

查看:129
本文介绍了批处理文件 - 文件写入变量列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得的所有文件(包括绝对路径)到一个变量,用空格分隔的列表。我的谷歌福似乎是弱在这方面,因为我一直运行到的问题。

我保存在%BASEDIR%基本目录,并想解析它的文件(不递归或包括子目录)。就像我提到的,这需要去到一个列表。我想像有一个可爱的小捷径,我可以使用,但一个for循环和串联会做的伎俩也。

在理想情况下,我想有这样的事情:

 回声的fileListC:\\ file1.c中C:\\ file2.c中C:\\ file3.c中


解决方案

pretty简单:

  SETLOCAL enabledelayedexpansion ENABLEEXTENSIONS
集列表=
在%% X(%BASEDIR%\\ *)做设置LIST =!LIST! %%X
集LIST =%LIST:〜1%

事实上,你会发现这个非常例子也帮助了设置命令,获取通过帮助制定 ,配有一个解释,为什么幼稚的做法是行不通的。

要使用一组不同的文件(而不是所有的),你可以很容易地改变通配符:

  %%用于中的x(%BASEDIR%\\ *。c)段设置LIST =!LIST! %%X

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.

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

解决方案

Pretty simple:

setlocal enabledelayedexpansion enableextensions
set LIST=
for %%x in (%baseDir%\*) do set LIST=!LIST! %%x
set LIST=%LIST:~1%

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆