• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

Batch File Problem


I'm sure I'm missing something amazingly simple here, but my head is about to explode and I need help.
I have 2 simple batch files;

cacls2.bat AA409
cacls2.bat AA411

cacls d:\users\%1 /t /e /G %1:f

but the result I get is :

D:\Users>cacls2.bat AA409

D:\Users>cacls d:\users\AA409 /t /e /G AA409:f

D:\Users>cacls2.bat AA409

D:\Users>cacls d:\users\AA409 /t /e /G AA409:f

D:\Users>cacls2.bat AA409

D:\Users>cacls d:\users\AA409 /t /e /G AA409:f

D:\Users>cacls2.bat AA409

D:\Users>cacls d:\users\AA409 /t /e /G AA409:f

D:\Users>cacls2.bat AA409

D:\Users>cacls d:\users\AA409 /t /e /G AA409:f

D:\Users>cacls2.bat AA409
^CTerminate batch job (Y/N)? y

It appears to be running the same first line over and over again!
It should run 107 different lines.


  • 4
  • 4
  • 4
2 Solutions
Lee W, MVPTechnology and Business Process AdvisorCommented:
Can who post the entire code of the batch file?  Sounds like you've got a loop in there that shouldn't be or that isn't exiting appropriately.
I would just do this in VBScript...

Set WS = CreateObject("WScript.Shell")
i = 409
control = 1
Ddo While (control<108)
     ws.run("cacls d:\users\" & i & " /t /e /G AA409:f")
     i = i + 2
     control = control + 1
     wscript.sleep 2500


That should do it, assuming that you are going by two's with your directories (as per your example).

Let me know if that did it for you.
NermalAuthor Commented:
Entire cacls.bat file:

cacls2.bat AA409
cacls2.bat AA411
cacls2.bat AA415
cacls2.bat ADF26
cacls2.bat AER47
cacls2.bat AF333
cacls2.bat AIM26
cacls2.bat AKB29
cacls2.bat ALW48
cacls2.bat AM622
cacls2.bat AMM203
cacls2.bat AN234
cacls2.bat APS41
cacls2.bat ARC48
cacls2.bat AW362
cacls2.bat BDW27
cacls2.bat BGC24
cacls2.bat BWB22
cacls2.bat CJF41
cacls2.bat CKH24
cacls2.bat DC282
cacls2.bat DC363
cacls2.bat DHL25
cacls2.bat DM368
cacls2.bat DMM34
cacls2.bat DN253
cacls2.bat DV217
cacls2.bat DW293
cacls2.bat EB258
cacls2.bat EB333
cacls2.bat EHS27
cacls2.bat EJG51
cacls2.bat EKT27
cacls2.bat EOS21
cacls2.bat ES339
cacls2.bat ET264
cacls2.bat FF219
cacls2.bat FI211
cacls2.bat GN216
cacls2.bat HA269
cacls2.bat HML30
cacls2.bat HUAP2
cacls2.bat IB249
cacls2.bat IB250
cacls2.bat JE261
cacls2.bat JEW35
cacls2.bat JJ270
cacls2.bat JJE26
cacls2.bat JO275
cacls2.bat JQ204
cacls2.bat JRS51
cacls2.bat JWAW2
cacls2.bat KAR32
cacls2.bat KB341
cacls2.bat KC314
cacls2.bat KDN20
cacls2.bat KK322
cacls2.bat KL296
cacls2.bat KNT22
cacls2.bat KPSB2
cacls2.bat LDG21
cacls2.bat LS299
cacls2.bat LW314
cacls2.bat MCN23
cacls2.bat MMN22
cacls2.bat MO284
cacls2.bat MPD30
cacls2.bat MU225
cacls2.bat MZ243
cacls2.bat NAS40
cacls2.bat NJ204
cacls2.bat NJR29
cacls2.bat NTH23
cacls2.bat NW258
cacls2.bat OB234
cacls2.bat PH297
cacls2.bat PP264
cacls2.bat PPC25
cacls2.bat PS409
cacls2.bat PWH24
cacls2.bat QW209
cacls2.bat REM46
cacls2.bat RI222
cacls2.bat RJT51
cacls2.bat SAI24
cacls2.bat SD372
cacls2.bat SGG24
cacls2.bat SHB31
cacls2.bat SK391
cacls2.bat SLM49
cacls2.bat SM587
cacls2.bat SMDG3
cacls2.bat SNV20
cacls2.bat SO274
cacls2.bat SR395
cacls2.bat TB283
cacls2.bat TO241
cacls2.bat TRB32
cacls2.bat VH248
cacls2.bat VLAB2
cacls2.bat WKC26
cacls2.bat WS264
cacls2.bat XP207
cacls2.bat YP221
cacls2.bat YSH21
cacls2.bat YY248
cacls2.bat ZV203
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

NermalAuthor Commented:
we had previously tried a batch file which contained line like:

cacls d:\users\AA409 /t /e /G AA409:f
cacls d:\users\AA411 /t /e /G AA411:f

but that batch file caused the same problem, so is it a problem with calling the cacls.exe file from a batch file?

Ok.........  Well, you could do this then..

Take all those directory names, and remove the "cacls2.bat" in front of them, and then put them in a text file (make sure there are no leading or trailing spaces, only the directory names).  Then, use this:

Set oFS = CreateObject("Scripting.FileSystemObject")
Set WS = CreateObject("WScript.Shell")
Set SourceFile = oFS.OpenTextFile("C:\sourcefile.txt")
Do While SourceFile.AtEndOfStream = False
     Directory = SourceFile.ReadLine
     WS.Run("cacls d:\users\" & Directory & " /t /e /G AA409:f")
     WScript.Sleep 2500

That ought to do it for you.  I prefer to use XCACLS for this stuff myself, but whatever suits your need..

Lee W, MVPTechnology and Business Process AdvisorCommented:
First, create a text file, users.txt - put every user name you want to use in it, for example...


Then use the command:
for /f "tokens=1" %a in (users.txt) do @cacls d:\users\%a /t /e /g %a:f >> d:\users\cacls.log 2>>&1

This one command will cycle through all the users in the file users.txt and run cacls against them.  Further, it will log all output from cacls to a file called cacls.log in the d:\users folder.

Note: If you want to use for in a batch file, then replace all "%a" with "%%a"

For more information on using for, go to a cmd prompt and type "for /?"
Lee W, MVPTechnology and Business Process AdvisorCommented:
I'm guessing you've never used the "for" command - to more easily create that text file of users, just open the batch file, resave it as something else, and find and replace all "CACLS2.bat" with "" (nothing)
Lee W, MVPTechnology and Business Process AdvisorCommented:
Oh, and if you just want to see what the for command line as I wrote it will do, add the word "echo" immediately  following the @ symbol - ... do @echo cacls... and remote everything following and including the first set of >>.  I test a lot of my for scripts by echoing them back first.
Actually, expanding on what leew said, use this script instead.  You won't have to edit your original batch file:

Set oFS = CreateObject("Scripting.FileSystemObject")
Set WS = CreateObject("WScript.Shell")
Set SourceFile = oFS.OpenTextFile("C:\cacls.bat")
Do While SourceFile.AtEndOfStream = False
     Directory = Right(SourceFile.ReadLine, 5)
     WS.Run("cacls d:\users\" & Directory & " /t /e /G AA409:f")
     WScript.Sleep 2500

Also, it just dawned on me what your problem is...    You are calling "cacls d:\users..... blah blah" meaing cacls.EXE, but, you didn't specify an extension, so the system in running cacls.BAT repeatedly, since it is found first in the directory structure alphabetically....


NermalAuthor Commented:
many thanks for your help both, i will split the points equally as I have just discovered my fundemental mistake.
I called the batch file cacls.bat which has the same name as cacls.exe so when the batch file called cacls, it didn't call the executable it called the batch file.....which is where the loop comes in.

Many thanks.

See above.  :)
NermalAuthor Commented:
Damn, beat me to it.
I will split the points but not quite so equally!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 4
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now