批量获取目录路径的姓氏
Get the last name of a directory path in batch
我的任务是为我的公司编写安装脚本,以便能够在用户计算机上安装打印机驱动程序。
到目前为止我所做的是让脚本接受一个参数(打印机类型 brother、xerox 等),它将收集驱动程序的相对路径,并且 运行 在输出所有驱动程序的路径上执行 forfiles
命令,之后它会要求用户提供正确的驱动程序,将其从网络驱动器复制到用户桌面,然后 运行 它。
我需要做的是 运行ning 部分,我需要将驱动复制到桌面,并以某种方式获取最后一个目录部分(例如:user\desktop\test.exe
我需要收集最后一个第 test.exe
部分)我怎样才能以实用的方式做到这一点?
基本上我需要通过 \
拆分路径并获取该列表的最后一个条目,这可以批量进行吗?
@echo off
type banner.txt
if [%1]==[] goto usage
:verify_argv
IF '%1'=='canon' GOTO get_canon_path
IF '%1'=='xerox' GOTO get_xerox_path
IF '%1'=='hp' GOTO get_hp_path
IF '%1'=='dell' GOTO get_dell_path
IF '%1'=='brother' GOTO get_brother_path
goto :eof
:get_canon_path
set dir_path=\mgtutils01\windows7apps\PRINTERS\Canon
goto :install_drivers
:get_xerox_path
set dir_path=\mgtutils01\windows7apps\PRINTERS\Xerox
goto :install_drivers
:get_hp_path
set dir_path=\mgtutils01\windows7apps\PRINTERS\HP
goto :install_drivers
:get_dell_path
set dir_path=\mgtutils01\windows7apps\PRINTERS\Dell
goto :install_drivers
:get_brother_path
set dir_path=\mgtutils01\windows7apps\Brother\Drivers
goto :install_drivers
:usage
@echo Usage: .\driver [PRINTER_TYPE]
exit /B 1
:install_drivers
@echo Finding drivers...
pushd "%dir_path%"
forfiles /s /m *.exe /c "cmd /c echo @relpath"
set /p to_install="Copy the path of the correct driver and paste here: "
@echo Copying file to %USERPROFILE%, please wait..
xcopy %to_install% "%USERPROFILE%\Desktop"
@echo Installing driver..
pushd "%USERPROFILE%\Desktop"
要获取文件或目录路径的最后一个元素,您可以:
要么使用 for
loop 及其 ~
修饰符:
set "ITEM=user\desktop\test.exe"
for %%I in ("%ITEM%") do set "NAME=%%~nxI"
echo %NAME%
或通过call
command调用子例程,将路径作为参数传递并再次使用~
修饰符:
set "ITEM=user\desktop\test.exe"
call :SUB "%ITEM%"
goto :EOF
:SUB
set "NAME=%~nx1"
goto :EOF
对于这两种变体,~nx
部分从存储在引用 (%%I
或 %1
)。在命令提示符 window 中键入 for /?
和 call /?
并阅读帮助文本;您将分别找到 for
变量引用和参数引用的所有可能的 ~
修饰符。
我的任务是为我的公司编写安装脚本,以便能够在用户计算机上安装打印机驱动程序。
到目前为止我所做的是让脚本接受一个参数(打印机类型 brother、xerox 等),它将收集驱动程序的相对路径,并且 运行 在输出所有驱动程序的路径上执行 forfiles
命令,之后它会要求用户提供正确的驱动程序,将其从网络驱动器复制到用户桌面,然后 运行 它。
我需要做的是 运行ning 部分,我需要将驱动复制到桌面,并以某种方式获取最后一个目录部分(例如:user\desktop\test.exe
我需要收集最后一个第 test.exe
部分)我怎样才能以实用的方式做到这一点?
基本上我需要通过 \
拆分路径并获取该列表的最后一个条目,这可以批量进行吗?
@echo off
type banner.txt
if [%1]==[] goto usage
:verify_argv
IF '%1'=='canon' GOTO get_canon_path
IF '%1'=='xerox' GOTO get_xerox_path
IF '%1'=='hp' GOTO get_hp_path
IF '%1'=='dell' GOTO get_dell_path
IF '%1'=='brother' GOTO get_brother_path
goto :eof
:get_canon_path
set dir_path=\mgtutils01\windows7apps\PRINTERS\Canon
goto :install_drivers
:get_xerox_path
set dir_path=\mgtutils01\windows7apps\PRINTERS\Xerox
goto :install_drivers
:get_hp_path
set dir_path=\mgtutils01\windows7apps\PRINTERS\HP
goto :install_drivers
:get_dell_path
set dir_path=\mgtutils01\windows7apps\PRINTERS\Dell
goto :install_drivers
:get_brother_path
set dir_path=\mgtutils01\windows7apps\Brother\Drivers
goto :install_drivers
:usage
@echo Usage: .\driver [PRINTER_TYPE]
exit /B 1
:install_drivers
@echo Finding drivers...
pushd "%dir_path%"
forfiles /s /m *.exe /c "cmd /c echo @relpath"
set /p to_install="Copy the path of the correct driver and paste here: "
@echo Copying file to %USERPROFILE%, please wait..
xcopy %to_install% "%USERPROFILE%\Desktop"
@echo Installing driver..
pushd "%USERPROFILE%\Desktop"
要获取文件或目录路径的最后一个元素,您可以:
要么使用
for
loop 及其~
修饰符:set "ITEM=user\desktop\test.exe" for %%I in ("%ITEM%") do set "NAME=%%~nxI" echo %NAME%
或通过
call
command调用子例程,将路径作为参数传递并再次使用~
修饰符:set "ITEM=user\desktop\test.exe" call :SUB "%ITEM%" goto :EOF :SUB set "NAME=%~nx1" goto :EOF
对于这两种变体,~nx
部分从存储在引用 (%%I
或 %1
)。在命令提示符 window 中键入 for /?
和 call /?
并阅读帮助文本;您将分别找到 for
变量引用和参数引用的所有可能的 ~
修饰符。