none
How to create a folder, permission with the user id based on a CSV input file

    Question

  • I am looking for help to point me in the right direction. As a complete newbie I'm a little overwhelmed by the content on this site and I'm not sure where to start.

    Problem:
    1. I have a CSV file with a list of User IDs (i.e. jsmith3, mtodd99, etc)
    2. Based on this file, I would like a script to read this and create a user folder and a home sub-folder i.e. \\server\home$\jsmith3\home, \\server\home$\mtod99\home, etc
    3. Finally the script needs to permission the user folder with it's ID and RW rights i.e. jsmith3 is assigned to the ACL of \jsmith3 and it's given RW

    Now this is probably quite an indepth task so i'm not asking anyone to write it. What I need is some guidance on where to look to get started. I think I'm probably best to start with reading a text file and creating the folders before trying to tackle the permissioning.

    Thank you for you assistance.

    Monday, November 22, 2010 4:38 PM

Answers

All replies

  • It depends on the structure of your CSV file (or is it perhaps a straight text file?). If it is a text file then the task is trivial:

    @echo off
    for /F %%a in ('type d:\Users.txt') do (
       md "\\Server\home$\jsmith3\home"
       cacls "\\Server\home$\jsmith3"  /e  /t  /g  %%a:F
       pause
    )

    Remove the "pause" line after testing the batch file on one or two user names.

    Monday, November 22, 2010 4:50 PM
  • a plain text file is probably simplest for this initial first step

    I think I see what you're doing, but how do I replace 'jsmith3' with a variable, where the value for the variable is what is read in from the txt file?

    Monday, November 22, 2010 6:06 PM
  • Sorry, made a mistake there. The code should, of course, read

    @echo off
    for /F %%a in ('type d:\Users.txt') do (
       md "\\Server\home$\%%a\home"
       cacls "\\Server\home$\%%a"  /e  /t  /g  %%a:F
       pause
    )

    Monday, November 22, 2010 6:39 PM
  • haha np. i thought it was a test to see if i was following along. i did play around with it and came to the same solution as yourself (though i changed the cacls to :C to give the user modify rights instead of full control).

    i also added the following after @echo off

    set /p unc=Enter the UNC path
    and then changed the md command so that it now looks like:

    @echo off
    set /p unc=Enter the UNC path
    for /F %%a in (c:\scripts\Users.txt) do (
       md "%unc%\%%a\home"
       cacls "%unc%\%%a"  /e  /t  /g %%a:C
       pause
    )

    is that the correct or proper way to pass on a user input string? my thinking is this will let the user input the unc path of the folder creation

    Monday, November 22, 2010 7:45 PM
  • Yes, this is correct. To get around the guessing game you might want to change
    set /p unc=Enter the UNC path
    to
    set /p unc=Enter the UNC path, e.g. \\Server\home$:
    if "%unc%"=="" goto :eof


    The last line gives you a graceful exit in case you wish to get out.
    Monday, November 22, 2010 7:51 PM
  • Thank you Pegasus.

    I have a question regarding the cacls.exe command. Is there a way to capture errors during the permissioning process? My concern is if a user id is incorrect I want a log so I can manually investigate.

    I've checked the command's parameter listings on ss64.com and it doesn't seem to have this option.

    Monday, November 29, 2010 5:58 PM
  • All console commands let you capture their output, no matter what it is. It is called "output" redirection and it works like so:

    cacls xxxx  /.. /.. /..  1>d:\output.txt  2>&1

    "1>" captures standard output
    "2>" captures error output (which requires a different file name)
    "&1" allows you to direct standard and error output into the same file.

    Monday, November 29, 2010 8:53 PM