2013-07-22 113 views
1

我想在批處理文件中做下面的事情。批處理腳本 - 下面批處理文件中的錯誤在哪裏?

  1. Check if a folder exists
  2. If not, create the folder.
  3. If the folder has created then provide Read permission to Everyone
  4. Else show error that the folder has not created.

下面是批處理文件(test.bat的):

​​

但是在執行從CMD此test.bat的文件,我得到以下錯誤:

IF NOT EXIST "C:\Users\Test\AppData\Local\Temp\Data" (MKDIR "C:\Users\Test\AppData\Local\Temp\Data") 
(CI)R) was unexpected at this time. 
IF EXIST "C:\Users\Test\AppData\Local\Temp\Data" (
icacls "C:\Users\Test\AppData\Local\Temp\Data" /grant "Everyone":(OI)(CI)R) 

我可以看到該文件夾​​已創建,但未設置權限。你能告訴我爲什麼會出現這個錯誤嗎?謝謝 !

回答

2

爲什麼是一批有歧義的語法和不能告訴一個)之間的區別,關閉塊和)表現爲一個普通的字符的文件名或隨你。

HOW是與前面的插入符號逃脫「普通」字符^)所以"Everyone":(OI)(CI)R)變得"Everyone":(OI^)(CI^)R)

注意然而,該)以下的R閉合塊,因此應當NOT有插入符號但是這會引入進一步的語法錯誤 - 否則必須與前面的在同一條物理線路上因此您需要

IF EXIST %FolderPath% (icacls %FolderPath% /grant "Everyone":(OI^)(CI^)R 
) ELSE (
2

試試這個:

@ECHO OFF &SETLOCAL 
SET "folderpath=test" 
IF EXIST "%FolderPath%" icacls %FolderPath% /grant "Everyone":(OI)(CI)R 
IF NOT EXIST "%FolderPath%" (
    ECHO ERROR: The folder %FolderPath% can not be found 
    GOTO Finish 
)