Windows 批处理文件命令和变量
我不确定这是否可行,但我需要的是一个在 Windows 7 上运行的普通 bat/cmd 文件并执行以下操作:
步骤 1. findstr
- 它应该使用以下命令找到特定字符串正则表达式引擎。假设我们正在查找标签
中包含的数字(假设这样的文件是唯一的,因此返回一个值)。该命令会将 123
打印到屏幕上,但我需要将其保存在变量中(不知道如何)。
步骤 2. 在另一个目录上再次调用 findstr
。现在我们想要找到一个文件名称(/m
选项),其中包含我们在步骤 1 中保存的值(在另一组文件中,即另一个目录中)。再次将结果(文件名)保存在变量中。假设 file_123.txt
符合条件。
步骤 3. 将第二次 findstr
调用得到的文件 (file_123.txt
) 复制到另一个位置。
整个问题围绕着如何将 Windows 命令的结果保存到变量中,以便能够将这些值作为参数提供给后续命令。
I'm not sure whether it is possible, but what I need is a plain bat/cmd file that runs on windows 7 and does such things:
Step 1. findstr
- it should find a specific string using regular expressions engine. Suppose we're looking for a number enclosed in tags <id>123</id>
(suppose such a file is unique, so one value is returned). The command would print 123
to the screen, but I need to save it in a variable (don't know how).
Step 2. Another call to findstr
on another directory. Now we want to find a file NAME (/m
option) containing the value that we saved on Step 1 (in another group of files i.e. another directory). And again, save the result (name of the file) in a variable. Say, file_123.txt
matches the criteria.
Step 3. Copy the file that we got as a result of the second findstr
call (file_123.txt
) to another location.
The whole question turns around the point about how to save the result of windows commands to variables to be able to provide these values to subsequent commands as parameters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在变量中获取命令输出的一般方法是
(有各种变体,具体取决于上下文和确切的需求)。
至于你的步骤,我午饭后详细阐述。其中存在一些错综复杂的情况。
The general way of getting command output in variables is
(with various variations, depending on context and what exactly is desired).
As for your steps, I elaborate after lunch. There are some intricacies.
步骤 1:
了解分隔符会根据输出各部分的“分隔”内容(无论是空格、特殊字符等)而变化。
步骤 2 & 3:
Step 1:
Understand that delims will change depending on what 'separates' the parts of the output (whether its a space, a special character, etc.)
Step 2 & 3: