Announcement

Collapse
No announcement yet.

For loop problem

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • For loop problem

    Hi,

    I have a CMD file which extracts users from an Active Directory group then trims it so only the username is displayed in the output file GroupMemberTrim.txt.

    It works fine, however I would like to use dynamic file names instead of static

    GroupMemberExtract.txt -> %groupname%-Extract.txt
    GroupMemberTrim.txt -> %groupname%-Trim.txt

    When I change this it breaks the FOR loop so the %%a variable does not read in the contents of %groupname%-Extract.txt instead it reads the filename %groupname%-Extract.txt as the %%a variable.

    Does anyone have any ideas how this could be fixed?

    Thanks





    Code:
    @echo off
    CLS
    
    :GroupsMembersExtract
    echo.
    set /p groupname=enter groupname:
    echo.
    echo.
    dsquery * domainroot -filter "(&(objectClass=group)(name=""%groupname%""))" -l -d DomainName -attr member >>GroupMemberExtract.txt
    
    
    :GroupsMembersTrim
    CLS
    setlocal enabledelayedexpansion
    FOR /F "tokens=1 delims=," %%a IN ( GroupMemberExtract.txt ) DO (
    set oldName=%%a
    set newName=!oldName:~3!
    echo !%newName!>>GroupMemberTrim.txt
    )
    SETLOCAL DISABLEEXTENSIONS
    
    
    :END

  • #2
    Re: For loop problem

    Originally posted by brian873 View Post

    GroupMemberExtract.txt -> %groupname%-Extract.txt
    GroupMemberTrim.txt -> %groupname%-Trim.txt
    Code:
    [...]
    FOR /F "tokens=1 delims=," %%a IN ( GroupMemberExtract.txt ) DO (
    [...]
    Because a groupname can have embedded spaces in it, you should add a quote sign (") around the name "%groupname%-Trim.txt" and also everywhere around the name "%groupname%-Extract.txt".
    Then you should also edit the quoted line to
    FOR /F "usebackq tokens=1 delims=," %%a IN ("GroupMemberExtract.txt") DO (

    btw the creation of file GroupMemberExtract.txt is not really nessesary. It is possible to edit the script so it just have to create the last file.

    /Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: For loop problem

      Originally posted by Rems View Post

      btw the creation of file GroupMemberExtract.txt is not really nessesary. It is possible to edit the script so it just have to create the last file.

      Thanks for the reply.

      I have managed to get the script working with code below, which is a little neater.

      However I am puzzled how to get it working without the first text file.



      @echo off
      CLS
      echo.
      set /p groupname=enter groupname:
      echo.
      echo.

      dsquery * domainroot -filter "(&(objectClass=group)(name=""%groupname%""))" -l -d HMNET -attr member >>"%groupname%Extract.txt"
      FOR /F "USEBACKQ TOKENS=2 DELIMS==," %%# IN ("%groupname%Extract.txt") DO ECHO=%%#>>"%groupname%Trim.txt"

      Comment


      • #4
        Re: For loop problem

        Originally posted by brian873 View Post
        I am puzzled how to get it working without the first text file.
        By putting the dsquery as the For statement in an other For-Do loop. The existing For-Do loop wil then run within the new loop.

        It is also possible that Delayed Expansion does not have to be enabled, by using a call:label stament in your script.


        sorry can't show examles right now.

        /Rems

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: For loop problem

          Originally posted by Rems View Post
          By putting the dsquery as the For statement in an other For-Do loop. The existing For-Do loop wil then run within the new loop.
          example:
          Code:
          @echo off & CLS
          
          set /p "groupname=enter groupname: "
          call:Members %groupname%
          
          :end of batch
          
          goto:EOF  + + + sub routines + + +
          
          :Members
          >"%*-members.txt" call:GroupsMembersExtract %*
          exit /b 0
          
          :GroupsMembersExtract
          For /f "tokens=*" %%! in (
             'dsquery * domainroot -filter "(&(objectClass=group)(name=""%*""))" -l -attr member'
             ) do call:GroupsMembersTrim "%%!"
          exit /b 0
          
          :GroupsMembersTrim
          For /f "tokens=2 delims==" %%* in ('echo.%*') do set "cn=%%*"
          call echo.%cn:~0,-3%
          exit /b 0
          The label :Members is optiona. I have added it to remove any accidentally typed leading or trailing spaces from the groupname entered.


          /Rems

          This posting is provided "AS IS" with no warranties, and confers no rights.

          __________________

          ** Remember to give credit where credit's due **
          and leave Reputation Points for meaningful posts

          Comment

          Working...
          X