解析参数,这是一个逗号分隔的字符串,传递到批处理文件



我有一个批处理文件,它接受一个参数,这是一个逗号分隔的字符串,我这样称呼它:

myBatch.bat ".git,*.tmp,file.c"

批处理文件中,我需要编写一个代码来解析在命令行传递的参数并像这样中断它:

@echo off
set excludes=%~1
set excludes_cmd=""
FOR /f "tokens=1* delims=," %%a IN ("%excludes%") DO (
   set "excludes_cmd=%%excludes_cmd%% --exclude %%~a"
)

所以最后,当我选择"excludes_cmd"变量时,我会得到这个:

--exclude .git --exclude *.tmp --exclude file.c

你应该能够通过基本的变量扩展和替换来做到这一点:

@Echo Off
Set "excludes=%~1"
If Not Defined excludes GoTo :EOF
Set "excludes_cmd=--exclude %excludes:,= --exclude %"

变量 %excludes_cmd% 应包含所需的内容。

想出了与 Compo 类似的方法,只是插入您的示例作为默认值。

@echo off
if "%~1"=="" "%~f0" ".git,*.tmp,file.c"
set "excludes=,%~1"
set "excludes_cmd=%excludes:,= --exclude %"
set excludes
excludes=,.git,*.tmp,file.c
excludes_cmd= --exclude .git --exclude *.tmp --exclude file.c

最新更新