批处理程序中获取路径
批处理程序 D:\Desktop\echoPath.bat
@echo off
echo Current Folder : %%cd%% : %cd%
echo File Name Only : %%~n0 : %~n0
echo File Extension : %%~x0 : %~x0
echo Name in 8.3 notation : %%~sn0 : %~sn0
echo File Attributes : %%~a0 : %~a0
echo Located on Drive : %%~d0 : %~d0
echo File Size : %%~z0 : %~z0
echo Last-Modified Date : %%~t0 : %~t0
echo Parent Folder : %%~dp0 : %~dp0
echo Fully Qualified Path : %%~f0 : %~f0
echo FQP in 8.3 notation : %%~sf0 : %~sf0
pause
在D:\Downloads 文件夹下执行 D:\Desktop\echoPath.bat,输出如下
D:\Downloads>D:\Desktop\echoPath.bat
Current Folder : %cd% : D:\Downloads
File Name Only : %~n0 : echoPath
File Extension : %~x0 : .bat
Name in 8.3 notation : %~sn0 : echoPath
File Attributes : %~a0 : --a------
Located on Drive : %~d0 : D:
File Size : %~z0 : 695
Last-Modified Date : %~t0 : 2017/09/06 14:08
Parent Folder : %~dp0 : D:\Desktop\
Fully Qualified Path : %~f0 : D:\Desktop\echoPath.bat
FQP in 8.3 notation : %~sf0 : D:\Desktop\echoPath.bat
请按任意键继续. . .
批处理程序获取命令行参数
方法一
批处理程序 D:\Desktop\echoCmdLineArgs.bat
@echo off
set CMD_LINE_ARGS=
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
:doneSetArgs
echo Command line Args: %CMD_LINE_ARGS%
pause
执行程序,并输入参数,输出如下:
D:\Desktop>echoCmdLineArgs.bat arg1 arg2 arg3
Command line Args: arg1 arg2 arg3
请按任意键继续. . .
方法二
批处理程序 D:\Desktop\echoCmdLineArgs2.bat
@echo off & setlocal enabledelayedexpansion
set CMD_LINE_ARGS=
for %%j in (%*) do set CMD_LINE_ARGS=!CMD_LINE_ARGS! %%j
echo Command line Args : %CMD_LINE_ARGS%
pause
执行程序,并输入参数,输出如下:
D:\Desktop>echoCmdLineArgs2.bat arg1 arg2 arg3
Command line Args : arg1 arg2 arg3
请按任意键继续. . .
跨驱动切换目录
C:\Windows\System32>cd /D D:\Desktop
D:\Desktop>