Batch file 脚本编写练习

Batch file 脚本编写练习,batch-file,cmd,Batch File,Cmd,我正在尝试完成一项家庭作业脚本作业,我基本上没有考虑过这项作业,需要找到/研究脚本的修复方法。我需要运行的脚本查看一个三列(名称、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

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

我相信文件CUG是好的,但文件一一直给我错误。如果有人能帮我,我会非常感激的。我已经尝试了我能想到的一切,但我被卡住了

干杯

詹姆斯

有几个错误:

对于(Department.txt)中的/f%%a,请致电CUG

  • 在这种情况下,您可以不带参数地调用
    CUG
    ,但可以使用
    %1
    。 您应该使
    CUG
    成为一个子例程,而不是另一个文件,并将参数传递给
    CUG
  • CUG
    文件中重新使用
    %LastGroupCreated%
    您需要
    在文件开头设置本地NABLEDELAYEDEXPANSION
  • FOR/f“tokens=2,3 skip=4”%%A IN(%userfFile%)DO CALL
    -调用什么

  • 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
    
    @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