在批处理文件中使用开关
我有一个批处理文件,我需要像“mybatch.bat -rc:\mydir”一样调用它,并且批处理文件循环遍历目录并将文件名写入输出。我面临的问题是我无法读取参数“-r”。
它看起来像这样:
@echo off
echo [%date% %time%] VERBOSE START
for %%X in (%1\*.xml) do echo [%date% %time%] VERBOSE systemmsg Parsing XML file '%%X'
echo [%date% %time%] VERBOSE END
但是我可以使用 %2 而不是 %1 并且一切正常,但我想按参数读取。这可能吗?
干杯!
I have a batch file and I need to invoke it like this "mybatch.bat -r c:\mydir", and the batch file loops through the directory and writes file names to the output. The problem I'm facing is that I cannot read parameter "-r".
Here's what it looks like:
@echo off
echo [%date% %time%] VERBOSE START
for %%X in (%1\*.xml) do echo [%date% %time%] VERBOSE systemmsg Parsing XML file '%%X'
echo [%date% %time%] VERBOSE END
I can however use %2 instead of %1 and all works fine, but I want to read by parameter. Is this possible?
Cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不完全确定我看到了你的问题。在这种情况下,
%1
显然是-r
并且您应该使用%2
,即c :\mydir
。如果您的意思是要确保用户首先指定
-r
,您可以使用类似以下内容:如果
-r
是可选,则您通常可以这样做:然后使用
rflag
和fspec
。批量进行真正的位置无关参数解析并不是一件容易的事。我们有一个子系统可以做到这一点,但不幸的是,它是专有的。我会告诉你,它跑了大约80多条线,并不是世界上最快的野兽。
我的建议是对参数格式施加严格的要求,而不是走与位置无关的道路。你会省去很多麻烦:-)
I'm not entirely sure I see your problem.
%1
in this case is clearly-r
and you should be using%2
which isc:\mydir
.If you mean you want to ensure that the user specifies
-r
first, you can use something like:If the
-r
is optional, you can often do:and then use
rflag
andfspec
.Doing true position-independent parameter parsing in batch is not an easy job. We have a subsystem which does it but unfortunately, it's proprietary. I will tell you it runs across about 80-odd lines and is not the fastest beast in the world.
My advice would be to impose strict requirements of the argument formats rather than go down the position-independent path. You'll save yourself a lot of hassles :-)
为什么不能将批处理文件简单地称为
mybatch.bat c:\mydir
,然后仅评估%1
,然后完成?Why can't you call your batchfile simply as
mybatch.bat c:\mydir
, then just evaluate%1
, and be done?如果批处理文件的唯一目的是将文件名写入输出,那么您不能简单地使用 dir /b /s 吗?
If the only purpose of the batchfile is to write the filenames to output, can't you simply use
dir /b /s
?