在批处理文件中使用开关

发布于 2024-09-13 02:50:15 字数 370 浏览 2 评论 0原文

我有一个批处理文件,我需要像“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

债姬 2024-09-20 02:50:15

我不完全确定我看到了你的问题。在这种情况下,%1 显然是 -r 并且您应该使用 %2,即 c :\mydir

如果您的意思是要确保用户首先指定 -r,您可以使用类似以下内容:

@echo off
if not "x%1"=="x-r" (
    echo [%date% %time%] ERROR Called without -r
    goto :eof
)
echo [%date% %time%] VERBOSE    START
for %%X in (%2\*.xml) do echo [%date% %time%] VERBOSE systemmsg Parsing file '%%X'
echo [%date% %time%] VERBOSE    END

如果 -r可选,则您通常可以这样做:

set fspec=%1
set rflag=no
if "x%fspec"=="x-r" (
    set fspec=%2
    set rflag=yes
)

然后使用rflagfspec

批量进行真正的位置无关参数解析并不是一件容易的事。我们有一个子系统可以做到这一点,但不幸的是,它是专有的。我告诉你,它跑了大约80多条线,并不是世界上最快的野兽。

我的建议是对参数格式施加严格的要求,而不是走与位置无关的道路。你会省去很多麻烦:-)

I'm not entirely sure I see your problem. %1 in this case is clearly -r and you should be using %2 which is c:\mydir.

If you mean you want to ensure that the user specifies -r first, you can use something like:

@echo off
if not "x%1"=="x-r" (
    echo [%date% %time%] ERROR Called without -r
    goto :eof
)
echo [%date% %time%] VERBOSE    START
for %%X in (%2\*.xml) do echo [%date% %time%] VERBOSE systemmsg Parsing file '%%X'
echo [%date% %time%] VERBOSE    END

If the -r is optional, you can often do:

set fspec=%1
set rflag=no
if "x%fspec"=="x-r" (
    set fspec=%2
    set rflag=yes
)

and then use rflag and fspec.

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 :-)

半边脸i 2024-09-20 02:50:15

为什么不能将批处理文件简单地称为 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?

删除→记忆 2024-09-20 02:50:15

如果批处理文件的唯一目的是将文件名写入输出,那么您不能简单地使用 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 ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文