Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Cacls and a batch file...  (Read 12930 times)

0 Members and 1 Guest are viewing this topic.

RapedApe

  • Guest
Cacls and a batch file...
« on: June 19, 2006, 09:52:54 PM »
I'm trying to create a little batch file that will automatically cacls all files whose directories are stored in another txt file. For example, I'll have a file called cacls.txt and in it there will be numerous files listed along with their directories. I want to be able to have the batch file read the lines and feed them into the cacls command to automatically set the permissions. I've been learning this as I've been going along and doing pretty good so far but this one's got me. O and one other question. Would there be a way to have it output which files it found and changed permissions on to a txt file?

Thanx

GuruGary



    Adviser
    Re: Cacls and a batch file...
    « Reply #1 on: June 19, 2006, 10:45:18 PM »
    You could use something like the following in a batch file (assuming you wanted to give full access to the Administrator user):
    Code: [Select]
    @echo off
    for /f "delims=" %%a in ('type cacls.txt') do echo y|cacls.exe "%%a" /G Administrator:F
    To log the results to a file you could do something like:
    Code: [Select]
    @echo off
    for /f "delims=" %%a in ('type cacls.txt') do echo y|cacls.exe "%%a" /G Administrator:F >>cacls.log
    Or only processed files to the log like:
    Code: [Select]
    @echo off
    for /f "delims=" %%a in ('type cacls.txt') do echo y|cacls.exe "%%a" /G Administrator:F|find /i "processed">>cacls.log

    If you have more questions, just let us know.

    RapedApe

    • Guest
    Re: Cacls and a batch file...
    « Reply #2 on: June 20, 2006, 10:27:19 AM »
    Doesn't seem to be working. Just brings up the document with the listed files in it. Then afterwards the permissions on the files haven't changed. I'm assuming the 'type cacls.txt' parts should be where my file of files to cacls are so I change that to thelist.txt right? Just testing it out now and not having any success. No luck on the logging part either but they might all be linked.

    RapedApe

    • Guest
    Re: Cacls and a batch file...
    « Reply #3 on: June 20, 2006, 02:12:05 PM »
    Nevermind, I actually managed to get that working. Couple other questions now though. Whenever I run the batch it brings up the txt file with the list of files in it. Is there a way to have it not bring the window up or just auto-close it? Also, anyway of editing the output to the log file? Just to make it a little more legible to the less technically inclined? If not this one isn't a big deal, I'm sure it'll be fine the way it is. That's it for now. And thanx for the previous response and for those soon to come.

    RapedApe

    • Guest
    Re: Cacls and a batch file...
    « Reply #4 on: June 21, 2006, 08:07:06 PM »
    Anybody?

    ghostdog74



      Specialist

      Thanked: 27
      Re: Cacls and a batch file...
      « Reply #5 on: June 21, 2006, 08:28:21 PM »
      there should not be a windows popping up.
      Code: [Select]
      for /f "delims=" %%a in (cacls.txt) do (
            echo y|cacls %%a /G Administrator:F >> log.txt
      )
      what exaxtly did you do?

      GuruGary



        Adviser
        Re: Cacls and a batch file...
        « Reply #6 on: June 21, 2006, 11:09:58 PM »
        Post the code you are using and we'll help you figure it out.

        RapedApe

        • Guest
        Re: Cacls and a batch file...
        « Reply #7 on: June 22, 2006, 07:55:45 AM »
        Code: [Select]
        for /f "delims=" %%a in (caclslist.txt) do (echo y|cacls "%%a" /p guest:n)
        That's it right now. I've been working on it a bit today and this is what I'm up to now. I've managed to make it work to the point where the popup doesn't come anymore. If I add ' to either side of caclslist.txt. IE 'caclslist.txt' it will pop-up again. So I managed to figure out why that was happening. The new problem is that it seems to repeat itself. Like it's going through the list twice. It actually goes through and displays a "The system cannot find the path specified." for every file that it didn't find. It looks like a problem with the logging line which is:

        Code: [Select]
        for /f "delims=" %%a in (type caclslist.txt) do echo y|cacls.exe "%%a" /p guest:n|find /i "processed">>cacls.log
        Any ideas?

        GuruGary



          Adviser
          Re: Cacls and a batch file...
          « Reply #8 on: June 22, 2006, 03:04:34 PM »
          You can send the error output to Null like:
          Code: [Select]
          for /f "delims=" %%a in (type caclslist.txt) do echo y|cacls.exe "%%a" /p guest:n|find /i "processed">>cacls.log 2>NUL

          RapedApe

          • Guest
          Re: Cacls and a batch file...
          « Reply #9 on: June 22, 2006, 05:23:03 PM »
          All I'm getting as far as an error message is whatever shows up on the screen. And it's not really much of a message, all I'm seeing is "The system cannot find the file type". On the plus side, the previous error, "The system cannot find the path specified.", is gone. However now I don't get anything logged in the cacls.log file.... 15 min later, I have figured out what's causing my problems. It's the ' around 'type caclslist.txt' in the logging line. Everytime it's there it will log whatever it finds but it'll duplicate. If I pull the ' then it will no longer duplicate but give the "The system cannot find the file type" error message and won't log them.

          Little something on the side here too, hopefully this one won't cause so much problem. I also want to have it setup to output any files it deletes or renames to the log file too. Here is the code I've got for both of those respectively.

          Code: [Select]
          for /f %%a in (caclslist.txt) do echo y|del "%%a" -y
          Code: [Select]
          for /f %%a in (caclslist.txt) do echo y|ren "%%a" "%%a".bad

          GuruGary



            Adviser
            Re: Cacls and a batch file...
            « Reply #10 on: June 22, 2006, 06:32:08 PM »
            Your FOR loop should either be like:
            Code: [Select]
            for /f "delims=" %%a in (caclslist.txt) do echo y|cacls.exe "%%a" /p guest:n|find /i "processed">>cacls.log 2>NUL which is probably preferred
            or
            Code: [Select]
            for /f "delims=" %%a in ('type caclslist.txt') do echo y|cacls.exe "%%a" /p guest:n|find /i "processed">>cacls.log 2>NUL
            Notice that if using only the file name you use no quotes and no command, or if using the TYPE command use single quotes.

            GuruGary



              Adviser
              Re: Cacls and a batch file...
              « Reply #11 on: June 22, 2006, 06:40:29 PM »
              For your other 2 questions I would use something like:
              Deleteing line:
              Code: [Select]
              for /f %%a in (caclslist.txt) do echo deleting "%%a" >>cacls.log 2>NUL&del /q /f "%%a"Renaming line (using MOVE instead of REN because REN does not have switch to supress overwrite)
              Code: [Select]
              for /f %%a in (caclslist.txt) do echo renaming "%%a" >>cacls.log 2>NUL&move /y "%%a" "%%a.bad"Also, note that if any of the file names or paths listed in the caclslist.txt have spaces in them, then you will want to include the "delims=" in your FOR loop (for all 3 answers).

              RapedApe

              • Guest
              Re: Cacls and a batch file...
              « Reply #12 on: June 22, 2006, 07:08:19 PM »
              The first two put me right back in the same position that I was before. It will save the log but on the command prompt window it will go through all the same files again. Here's the full code for that portion in case there's something wrong in the first line.

              Code: [Select]
              for /f "delims=" %%a in (caclslist.txt) do (echo y|cacls "%%a" /p guest:n)
              for /f "delims=" %%a in (caclslist.txt) do echo y|cacls.exe "%%a" /p guest:n|find /i "processed">>cacls.log 2>NUL

              I haven't tried the other two out but I'll give that a go right away. And thanx again for spending so much time trying to help me out with this.

              RapedApe

              • Guest
              Re: Cacls and a batch file...
              « Reply #13 on: June 22, 2006, 07:30:28 PM »
              The line for logging deleted files logs every file it tried to delete, whether it was there or not. The rename one does the same.

              GuruGary



                Adviser
                Re: Cacls and a batch file...
                « Reply #14 on: June 23, 2006, 10:24:47 AM »
                If you only want it to log files that are being deleted, you can try:

                Code: [Select]
                for /f %%a in (caclslist.txt) do if exist "%%a" (
                   echo deleting "%%a" >>cacls.log 2>NUL
                   del /q /f "%%a"
                   )

                and

                Code: [Select]
                for /f %%a in (caclslist.txt) do fi exist "%%a" (
                   echo renaming "%%a" >>cacls.log 2>NUL
                   move /y "%%a" "%%a.bad"
                   )