这里发生了什么?我完全不知道为什么批次不正确"正确"转移我的论点。
这里是foo.bat:
@echo off
echo %~dp0
shift
echo %~dp0
shift
echo %~dp0
这是我用param运行时会发生什么:
C:\Users\Public>foo bar
C:\Users\Public\
C:\Users\Public\
ECHO is off.
更期待这些案例':
C:\Users\Public>foo c:/this/is/some/other/path
C:\Users\Public\
c:\this\is\some\other\
ECHO is off.
和
C:\Users\Public>foo this/is/some/other/path
C:\Users\Public\
C:\Users\Public\this\is\some\other\
ECHO is off.
但有人可以澄清究竟发生了什么吗?我想通过它来解释' bar'作为一个驱动器和路径,但只是作为' bar'在我的第一个案例......
答案 0 :(得分:1)
@ECHO OFF
SETLOCAL
ECHO params=%*
echo The drivename and PATH of the file "%~0" is %~dp0
shift
echo The drivename and PATH of the file "%~0" is %~dp0
shift
echo The drivename and PATH of the file "%~0" is %~dp0
GOTO :EOF
也许这个更加扩展的版本可以帮助理解正在发生的事情。
如果没有提供明确的目录, cmd
会假设in the current directory
...
答案 1 :(得分:0)
它正在做它应该做的事情。
这与转移无关。
您正在回应酒吧的驱动器和路径。由于条形图不是文件,它会显示空白驱动器和路径。
"C:\Users\User\Desktop\b.bat" c:\windows\win.ini bar
在bat文件中
echo %~dp1
echo %~dp2
答案 2 :(得分:0)
SHIFT在依赖%~dp0
,%0
或类似脚本的脚本中应谨慎使用。考虑以下示例:
@ECHO OFF
SETLOCAL
:start params review
echo.
echo starting ...
ECHO params=%*
echo param0=%0
echo full path is %~dp0
echo param1=%1
echo param2=%2
echo param3=%3
echo param4=%4
echo param5=%5
shift
if [%~1]==[] goto:EOF
goto :start
GOTO:EOF
C:\Program Files (x86)>test.bat a -b /c
starting ...
params=a -b /c
param0=test.bat
full path is C:\Program Files (x86)\
param1=a
param2=-b
param3=/c
param4=
param5=
starting ...
params=a -b /c
param0=a
full path is C:\Program Files (x86)\
param1=-b
param2=/c
param3=
param4=
param5=
starting ...
params=a -b /c
param0=-b
full path is C:\Program Files (x86)\
param1=/c
param2=
param3=
param4=
param5=
这很好,符合预期,但是,如果我们尝试使用一组不同的参数,则一旦将“ path like”参数移到位置0,事情就会出错:
C:\Program Files (x86)>test.bat a /b -c
starting ...
params=a /b -c
param0=test.bat
full path is C:\Program Files (x86)\
param1=a
param2=/b
param3=-c
param4=
param5=
starting ...
params=a /b -c
param0=a
full path is C:\Program Files (x86)\
param1=/b
param2=-c
param3=
param4=
param5=
starting ...
params=a /b -c
param0=/b
full path is C:\
param1=-c
param2=
param3=
param4=
param5=
该解决方案如SS64.com中所述,可以使用SHIFT /1
@ECHO OFF
SETLOCAL
:start params review
echo.
echo starting ...
ECHO params=%*
echo param0=%0
echo full path is %~dp0
echo param1=%1
echo param2=%2
echo param3=%3
echo param4=%4
echo param5=%5
shift
if [%~1]==[] goto:EOF
goto :start
GOTO:EOF
C:\Program Files (x86)>test.bat a /b -c
starting ...
params=a /b -c
param0=test.bat
full path is C:\Program Files (x86)\
param1=a
param2=/b
param3=-c
param4=
param5=
starting ...
params=a /b -c
param0=test.bat
full path is C:\Program Files (x86)\
param1=/b
param2=-c
param3=
param4=
param5=
starting ...
params=a /b -c
param0=test.bat
full path is C:\Program Files (x86)\
param1=-c
param2=
param3=
param4=
param5=