Solved

Batch File Problem

Posted on 2004-09-14
12
263 Views
Last Modified: 2008-02-01
Hi,

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;

1)cacls.bat
cacls2.bat AA409
cacls2.bat AA411
...

2)cacls2.bat
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.

Help...
Andrew

0
Comment
Question by:Nermal
  • 4
  • 4
  • 4
12 Comments
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 12055311
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.
0
 
LVL 18

Expert Comment

by:exx1976
ID: 12055343
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
Loop


Viola!

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.
0
 
LVL 1

Author Comment

by:Nermal
ID: 12055537
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
0
 
LVL 1

Author Comment

by:Nermal
ID: 12055634
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
etc...

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

Thanks
Andrew
0
 
LVL 18

Expert Comment

by:exx1976
ID: 12055824
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
Loop
SourceFile.Close

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


HTH,
exx
0
 
LVL 95

Accepted Solution

by:
Lee W, MVP earned 150 total points
ID: 12055834
First, create a text file, users.txt - put every user name you want to use in it, for example...

AA409
AA411
AA415
ADF26
AER47
AF333
...
...

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 /?"
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 12055851
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)
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 12055878
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.
0
 
LVL 18

Assisted Solution

by:exx1976
exx1976 earned 350 total points
ID: 12055903
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
Loop
SourceFile.Close


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....

:-D

HTH,
exx
0
 
LVL 1

Author Comment

by:Nermal
ID: 12055925
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.

Andrew
0
 
LVL 18

Expert Comment

by:exx1976
ID: 12055950
See above.  :)
0
 
LVL 1

Author Comment

by:Nermal
ID: 12055966
Damn, beat me to it.
I will split the points but not quite so equally!

Thanks
Andrew
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
In this article, I will show you HOW TO: Create your first Windows Virtual Machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, the Windows OS we will install is Windows Server 2016.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now