【发布时间】:2026-02-22 08:15:01
【问题描述】:
我想处理和重写传递给 Windows bat 文件的参数,然后调用另一个 bat 文件。更准确地说:我想用斜杠替换参数中的反斜杠。
示例:
first.bat C:\Users\myself\test.txt anotherParam
应该打电话
second.bat C:/Users/myself/test.txt anotherParam
到目前为止我的尝试:
@echo off
setlocal ENABLEDELAYEDEXPANSION
set argCount=0
for %%x in (%*) do (
set /A argCount+=1
call set argVec[!argCount!]=%%~x:\=/%%
)
echo Number of processed arguments: %argCount%
for /L %%i in (1,1,%argCount%) do echo %%i- "!argVec[%%i]!"
call second.bat %argVec%
问题:
- 循环中的字符串替换无法按预期工作
- 调用 second.bat 不提交参数
感谢您对此主题的建议。
【问题讨论】:
-
子字符串替换仅适用于普通环境变量,但不适用于
for元变量,如代码中的%%x... -
也许这个简单的解决方案可能有效:
set "params=%*"和call second.bat %params:\=/%
标签: batch-file parameter-passing windows-subsystem-for-linux script