2012-11-24 95 views
2

我正在嘗試完成一項家庭作業腳本編寫任務,對此我一直沒有任何意見,需要查找/研究腳本的修復。我需要運行的腳本查看三列(名稱,ID,部門)文件(我給出的),並假設應該創建新的組和用戶,並將用戶分配到正確的組。每個組只能創建一次。我的問題是使用帶有標記的FOR/f命令。腳本練習

文件1個

@ECHO OFF 

SET /p userfFile="what is the file that contains new hires: " 

REM Loop through the lines of the file 
REM and copy the the department names to a new file 
FOR /f "tokens=3 skip=4" %%b IN (%userfFile%) DO ECHO %%b >> Department.txt 

REM sort the file back into itself 
sort Department.txt /0 Department.txt 

REM a variable to keep track of the groups that have neen created 
SET LastGroupCreated=None 

FOR /f %%a IN (Department.txt) DO CALL CUG 

FOR /f "tokens=2,3 skip=4" %%A IN (%userfFile%) DO CALL 

REM Clean Up 
DEL Department.txt 
SET userfFile= 

檔案2 「CUG」

REM Have we already processed this name (%1) ? 
IF "%LastGroupCreated%"=="%1" GOTO :EOF 

REM Not Processed, so create and update the variable 
NET LOCALGROUP /ADD %1 
SET LastGroupCreated=%1 

我相信文件CUG是好的,但文件中的一個不斷給我的錯誤。如果有人能幫助我,我會非常感激。我已經試過,我能想到的一切,我堅持

乾杯

詹姆斯

+0

什麼錯誤? –

回答

0

幾個錯誤:

FOR /f %%a IN (Department.txt) DO CALL CUG

  1. 在這個給你打電話CUG不帶參數,但你確實使用%1。 您應該使CUG成爲子程序而不是其他文件並將參數傳遞給CUG
  2. in CUG重複使用的文件%LastGroupCreated%您需要在您的文件開頭爲SETLOCAL NABLEDELAYEDEXPANSION
  3. FOR /f "tokens=2,3 skip=4" %%A IN (%userfFile%) DO CALL - 打電話給什麼?


@ECHO OFF 
SETLOCAL ENABLEDELAYEDEXPANSION 
... 
FOR /f %%a IN (Department.txt) DO CALL :CUG %%a 
... 
goto :eof 

:CUG 
REM Have we already processed this name (%1) ? 
IF "!LastGroupCreated!"=="%1" GOTO :EOF 

REM Not Processed, so create and update the variable 
NET LOCALGROUP /ADD %1 
SET LastGroupCreated=%1 
goto :eof