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 很好,但文件一总是给我错误。如果有人可以帮助我,我将不胜感激。我已经尝试了所有我能想到的并且被卡住了

干杯

詹姆士

4

1 回答 1

0

几个错误:

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

  1. 在这个为你调用CUG不带参数,但你确实使用%1. 您应该创建CUG一个子例程而不是另一个文件并将参数传递给CUG.
  2. 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
于 2012-11-29T07:00:00.567 回答