简体   繁体   English

Windows 7剩余的批处理文件参数

[英]windows 7 remaining batch file arguments

In windows batch files, I know %1 is replaced with the first argument, %2 is replaced with the 2nd argument and %* is replaced with all arguments. 在Windows批处理文件中,我知道%1被第一个参数替换, %2被第二个参数替换, %*被所有参数替换。

Is there a way to get all the arguments after the 1st one? 有没有一种方法可以在第一个参数之后获取所有参数? (eg arguments 2-N) What about all the arguments after the 2nd one? (例如,参数2-N)第二个参数之后的所有参数呢?

The SHIFT command doesn't seem to affect %* . SHIFT命令似乎不会影响%*

@ECHO OFF
SETLOCAL
CALL :allafter 3 %*
ECHO args=%args%
GOTO :eof

:allafter
FOR /l %%a IN (1,1,%1) DO SHIFT
(SET args=)
:argloop
shift
IF NOT .%1==. SET args=%args% %1&GOTO argloop
IF DEFINED args SET args=%args:~1%
GOTO :eof

to get everything after the 3rd argument to ARGS ARGS的第三个参数之后得到所有东西


Edit - to take care of space-separated elements which may include commas 编辑-处理空格分隔的元素,其中可能包含逗号

@ECHO OFF
SETLOCAL
CALL :allafter 3 %*
ECHO args=%args%
CALL :allafter2 3 %*
ECHO args=%args%
GOTO :eof

:allafter
FOR /l %%a IN (1,1,%1) DO SHIFT
(SET args=)
:argloop
shift
IF NOT .%1==. SET args=%args% %~1&GOTO argloop
IF DEFINED args SET args=%args:~1%
GOTO :EOF

:allafter2
SET /a count=%1
SET args=%*
:arg2loop
SET oldargs=%args%
call SET args=%%args:*%1 =%%
IF "%args%"=="%oldargs%" (call SET args=%%args:*%1,=%%) ELSE (SET /a count-=1)
shift
IF %count% gtr -1 GOTO arg2loop
GOTO :EOF

Hmm- spoke too soon. 嗯-说得太早了。 This modified routine should play nicer. 修改后的例程应该更好玩。 The previous version treated what was to be defined as one argument one,two,three as three separate arguments when the remove-leading-n was invoked. 先前版本在调用remove-leading-n时将要定义为一个, one,two,three参数视为三个独立的参数。

Well, there is a way-ish... 好吧,有一种方式...

By using the /n switch on the shift command, you can sort of do something like it. 通过使用shift命令上的/n开关,您可以执行类似的操作。 However, it will delete all of the argument and put them into a certain variable (so you can't call %3 anymore without a for loop). 但是,它将删除所有参数并将它们放入某个变量中(因此,如果没有for循环,您将无法再调用%3 )。

@setlocal enableextensions
@echo off
:loop
if "%~2" equ "" goto end
set variable=%variable% %~2
shift /2
goto loop
:end
echo %1
echo %variable%
endlocal

To separate the parameters again just do a simple for loop (I'm sure you can find documentation on it somewhere). 要再次分离参数,只需执行一个简单的for循环(我相信您可以在某处找到它的文档)。

Solution without shift & goto : 解决方案,无需shiftgoto

@echo off &setlocal enabledelayedexpansion
set /a count=0
for %%i in (%*) do set /a count+=1
set "args="
for /l %%i in (2,1,%count%) do if defined args (call set "args=!args! %%%%i") else call set "args=%%%%i"
echo.%args%
endlocal
> type t.bat
@echo off
echo %*
for  /f "tokens=1,*delims= " %%i in ("%*") do echo %%j

> t a b c d e f,g h i
a b c d e f,g h i
b c d e f,g h i

> t a,a b c d e f,g h i
a,a b c d e f,g h i
b c d e f,g h i

>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM