我正试图完成一项家庭作业脚本作业,实际上我根本没有考虑到这一点,需要为脚本找到/研究一个修复程序。我需要运行的脚本查看一个三列(名称、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是好的,但文件一不断给我错误。如果有人能帮我做这件事我会很感激的。我已经试过了我能想到的一切并且被困住了。
干杯
詹姆斯
发布于 2012-11-29 07:00:00
很少有错误:
FOR /f %%a IN (Department.txt) DO CALL CUG
CUG,但是您确实使用了%1。您应该使CUG成为一个子例程,而不是一个不同的文件,并将一个参数传递给CUG。CUG文件中,您可以重用%LastGroupCreated%,您需要在文件开始时使用SETLOCAL NABLEDELAYEDEXPANSION。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 :eofhttps://stackoverflow.com/questions/13544406
复制相似问题