2015-12-05 80 views
0

所以,我創建了一個程序,它可以在桌面上創建和刪除文件夾。我遇到了一個問題,當我爲該名稱寫一個空格時,出現錯誤「請輸入有效的文件夾名稱!」所以它的工作原理,但如果我使用多個空格,它說「文件夾創建成功」,我想讓這個程序發生錯誤,當我只在文件夾的名稱中使用空格。請幫忙!如何識別批處理腳本中的空格鍵?

這裏是我的代碼的一部分(完整的代碼是132線長)

set Choice= 
set /p Choice="Choose an option: " 
if '"%Choice%"'=='"1"' goto masodik 
if '"%Choice%"'=='"2"' goto negyedik 
if '"%Choice%"'=='"3"' goto otodik 
if '"%Choice%"'=='"4"' goto harmadik 
if not '"%Choice%"'=='"1"' goto hiba2 
if not '"%Choice%"'=='"2"' goto hiba2 
if not '"%Choice%"'=='"3"' goto hiba2 
if not '"%Choice%"'=='"4"' goto hiba2 
:masodik 
cls 
echo Create a folder 
echo --------------- 
echo. 
cd "%systemdrive%/documents and settings/%username%/desktop" 
echo Enter the folder's name! 
echo. 
set /p mappaneve="The folder's name: " 
if "%mappaneve%" EQU "" goto hiba 
if EXIST "%mappaneve%" goto hiba3 
md "%mappaneve%" 
cls 
echo Create a folder 
echo --------------- 
echo. 
echo Successfully created "%mappaneve%"! 
timeout /t 3 >nul 
cls 
goto elso 
+2

@UNICYCLEZRDEBESTINDEHERE:爲什麼?三條線足以顯示問題 – Stephan

回答

0

報價在可變asignation用於確保空間的正確處理。除非你想使用它,否則不要在你的變量裏面加引號。

集/ P 「mappaneve =該文件夾的名稱:」

1
if "%mappaneve: =%" EQU "" goto hiba 

即與什麼替換所有的空格,如果結果是什麼然後輸入一定是所有空間(如果那是你要求的)

0

Magoo在他的簡短答案中發佈了正確的代碼。

下面是一個更長的答案,與批註代碼相比,批註代碼和其他解釋有關代碼中的各種改進。

set "UserChoice=" 
set /p "UserChoice=Choose an option: " 

if "%UserChoice%" == "1" goto masodik 
if "%UserChoice%" == "2" goto negyedik 
if "%UserChoice%" == "3" goto otodik 
if "%UserChoice%" == "4" goto harmadik 

rem None of the 4 valid numbers was entered. 
goto hiba2 

:masodik 
rem Set current directory to desktop directory of current user. 
cd /D "%USERPROFILE%\Desktop" 

cls 
echo Create a folder 
echo --------------- 
echo. 

rem Define a double quote as default value to be able to remove all 
rem double qotes from input string with no syntax error even if the 
rem user just hits key RETURN or ENTER without entering any string. 
set "mappaneve="" 
set /p "mappaneve=Enter folder name: " 

rem Remove all double quotes from entered folder name. 
set "mappaneve=%mappaneve:"=%" 

rem Check if user has entered anything at all and if entered folder name 
rem does not exist of only 1 or more spaces by using environment variable 
rem substitution which removes for string comparison all spaces from the 
rem entered folder name. 
if "%mappaneve%" == "" goto hiba 
if "%mappaneve: =%" == "" goto hiba 

rem Appending \* makes sure to test on existence of a folder and not a file. 
rem It does not matter if entered folder name ends already with a backslash. 
rem Does the folder already exist? 
if exist "%mappaneve%\*" goto hiba3 

rem Create the folder and verify if that was successful with 
rem saving error message into a temporary file for output below. 
md "%mappaneve%" 2>"%TEMP%\%~n0_FolderError.tmp" 
if errorlevel 1 goto FolderError 

rem On successful creation of the folder an empty file is 
rem created which should be removed before processing further. 
del "%TEMP%\%~n0_FolderError.tmp" 

cls 
echo Create a folder 
echo --------------- 
echo. 
echo Created successfully "%mappaneve%". 
timeout /t 3 >nul 
cls 
goto elso 

:FolderError 
cls 
echo Failed to create folder "%mappaneve%". 
echo. 
echo Error message: 
echo. 
type "%TEMP%\%~n0_FolderError.tmp" 
del "%TEMP%\%~n0_FolderError.tmp" 
echo. 
pause 

的改進是:

  1. 比較字符串與命令IF應該只使用周圍的雙引號來完成。單引號將從4個字符串比較中刪除。

    當然,如果批處理用戶輸入例如"2"(編號2帶雙引號),由於處理if ""2"" == "1" goto masodik引起的語法錯誤,批處理執行將由命令處理器退出。使用延遲變量擴展將是這個問題的一個解決方案。

  2. if not '"%Choice%"'==...的4行可以簡單地由goto hiba2替換,因爲它們在前4個字符串比較後都是完全正確的。

  3. choice(SS64文章)是一個標準的Windows命令。因此建議避免使用choice(Microsoft文章)作爲環境變量或標籤的名稱。 UserChoice(CamelCase拼寫更容易閱讀)被用來代替Choice

    命令選擇將是第一個用戶提示一個很不錯的選擇,因爲這時用戶無法進入一些比1,2,3種不同的,或4

  4. 有由Windows環境中預定義變量USERPROFILE包含用戶個人資料目錄的路徑,其中包含目錄Desktop和其他用戶帳戶相關目錄。應該使用這個變量而不是用其他變量構建路徑。

  5. Windows上的目錄分隔符是反斜槓字符而不是正斜槓字符。 Windows同時支持/而不是\的目錄路徑,但最好使用正確的目錄分隔符。

  6. 用戶的配置文件目錄可以位於與系統驅動器不同的驅動器上。或者執行批處理文件的驅動器與用戶配置文件目錄的驅動器不同。因此建議使用帶參數/D的命令CD,否則如果當前驅動器與用戶配置文件目錄的驅動器不同,則更改目錄可能會失敗。

  7. 建議在提示用戶或清除變量之前爲環境變量定義默認值。用戶可以點擊RETURNENTER在這種情況下,環境變量保持其值。

    這是爲Choice完成的,但遺忘了mappaneve

  8. 用戶可以用雙引號輸入文件夾名稱。因此,建議從輸入的文件夾名稱中刪除所有雙引號,否則由於進一步處理批處理文件時出現語法錯誤,批處理腳本處理將由命令處理器退出。

  9. 要真正測試文件夾的存在與if exist,不也是一個文件,有必要另行追加if exist\*的文件夾名稱可能是與用戶輸入的字符串也確實是現有文件的名稱。

  10. 由於文件夾名稱中存在一個或多個無效字符,文件已存在,或者用戶沒有創建文件夾的權限,所以創建文件夾可能失敗。

    如果用戶真的只輸入文件夾名稱而不是目錄路徑來創建任何目錄(樹),那麼當前目錄就是用戶的桌面目錄,最後一個原因非常不可能。

    因此建議您在創建文件夾時進行成功測試。

對於理解使用的命令以及它們如何工作,打開命令提示符窗口中,執行有下面的命令,並完全讀取顯示每個命令的所有幫助頁面非常謹慎。

  • cd /?
  • cls /?
  • del /?
  • echo /?
  • goto /?
  • if /?
  • md /?
  • rem /?
  • set /?
  • timeout /?
  • type /?

而且也可以參考微軟的文章: