2016-03-14 196 views
2

我想改進我的批處理文件以顯示我的USB隨身碟的所有隱藏文件夾和文件! 所以;我的問題是如何知道我的變量!MyUSB!是否定義或不繼續進入我的腳本或退出如果沒有定義?顯示我的USB隨身碟上的所有隱藏文件夾和文件

這裏是我的代碼:

@echo off 
cls & color 0A & echo. 
Mode con cols=75 lines=7 
Title Show all hidden folders and files on your USB key by Hackoo 2016 
::::::::::::::::::::::::::::::::::::::::: 
:: Automatically check & get admin rights 
::::::::::::::::::::::::::::::::::::::::: 
REM --> Check for permissions 
Reg query "HKU\S-1-5-19\Environment">nul 2>&1 
REM --> If error flag set, we do not have admin. 
if '%errorlevel%' NEQ '0' (
Echo. 
ECHO     ************************************** 
ECHO      Running Admin shell... Please wait... 
ECHO     ************************************** 

    goto UACPrompt 
) else (goto gotAdmin) 

:UACPrompt 
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs" 
    set params = %*:"="" 
    echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" 

    "%temp%\getadmin.vbs" 
    del "%temp%\getadmin.vbs" 
    exit /B 

:gotAdmin 
:::::::::::::::::::::::::::: 
::START 
:::::::::::::::::::::::::::: 
setlocal ENABLEDELAYEDEXPANSION 
Set TmpLog=Tmp.txt 
Set Log=USBCopyLog.txt 
If exist %TmpLog% Del %TmpLog% 
If exist %TmpLog% Del %TmpLog% 
for /f "tokens=2" %%i in ('wmic logicaldisk where "drivetype=2" ^|find /i ":"') do (echo %%i && Set MyUSB=%%i) 
cls 
echo. 
echo   ######################################################### 
echo     Votre cle usb connecte en tant que !MyUSB! 
echo   ######################################################### 
echo. 
pause 
cls 
If Defined !MyUSB! && Attrib -s -h -r !MyUSB!\*.* /S /D >> !TmpLog! 2>&1 || Exit /b 
Cmd /U /C Type !TmpLog! > !Log! 
Explorer "!MyUSB!\" 
+0

見'如果/'。特別是「如果定義」。您應該使用您正在使用的語言學習編程。批處理用於刪除或複製文件和啓動程序。我從1985年開始從REXX轉移到批處理。 –

+0

不能在開始時使用'set「MyUSB =」',並在if>!MyUSB「==」「'檢查它是否被定義。 –

回答

0

我認爲此代碼的工作:

@echo off 
cls & color 0A & echo. 
Mode con cols=75 lines=7 
Title Show all hidden folders and files on your USB key by Hackoo 2016 
::::::::::::::::::::::::::::::::::::::::: 
:: Automatically check & get admin rights 
::::::::::::::::::::::::::::::::::::::::: 
REM --> Check for permissions 
Reg query "HKU\S-1-5-19\Environment">nul 2>&1 
REM --> If error flag set, we do not have admin. 
if '%errorlevel%' NEQ '0' (
Echo. 
ECHO     ************************************** 
ECHO      Running Admin shell... Please wait... 
ECHO     ************************************** 

Goto UACPrompt 
) else (goto gotAdmin) 

:UACPrompt 
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs" 
    set params = %*:"="" 
    echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" 

    "%temp%\getadmin.vbs" 
    del "%temp%\getadmin.vbs" 
    exit /B 

:gotAdmin 
:::::::::::::::::::::::::::: 
::START 
:::::::::::::::::::::::::::: 
Set TmpLog=%tmp%\Tmp.txt 
Set Log=%tmp%\USBLog.txt 
If exist %TmpLog% Del %TmpLog% 
for /f "tokens=2" %%i in ('wmic logicaldisk where "drivetype=2" ^|find /i ":"') do (Set MyUSB=%%i) 
cls 
setlocal ENABLEDELAYEDEXPANSION 
set _drive=%MyUSB% 
If Exist !_drive! (
cls 
echo. 
echo   ######################################################### 
echo      Your usb key is connected as !_drive! 
echo   ######################################################### 
echo. 
pause 
Attrib -s -h -r !_drive!\*.* /S /D >> !TmpLog! 2>&1 
Cmd /U /C Type !TmpLog! > !Log! 
If exist !TmpLog! Del !TmpLog! 
Explorer "!_drive!\" 
) ELSE (
cls 
color 0C 
echo. 
echo   ######################################################### 
echo       Your usb key is not detected 
echo   ######################################################### 
echo. 
pause 
Exit 
) 
相關問題