我正在尝试完成一个家庭作业脚本作业,我基本上没有考虑过,需要找到/研究脚本修复。我需要运行的脚本查看三列(名称,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
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 :eof