Solved

How to cope with folders with spaces in a batch file

Posted on 2014-01-23
4
596 Views
Last Modified: 2014-01-23
Hello,
Looking for some help with a batch file I need to run urgently to set permissions on a number of folders.  

We have the following folder structure

Folder1
Folder1\Client1
Folder1\Client2
Folder1\Client3
Then within each client folder we have several sub folders
e.g.
Folder1\Client1\SubFolder1
Folder1\Client1\SubFolder2
Folder1\Client1\SubFolder3

I need to reset permissions on all the Subfolders in the example above and have the following code in a file called permissions.bat

for /f %%g in ('dir "f:\temp\" /ad /b') do for /f %%a in ('dir %%~fg /ad /b') do icacls 

"%%~fg\%%a" /grant:r domain\mygroup:(OI)(CI)M

Open in new window


This works fine but for Client and Subfolders that contain spaces it doesnt work.  For example

Folder1\Client1\Sub Folder 1

Would show up as

Folder1\Client1\Sub\

Closely followed by System cannot find the file specified error.  Any ideas?

This is running on a 2003 Server by the way.

Thanks for looking
0
Comment
Question by:cloughs
4 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39804980
I think you missed the required double quotes in some of the code
for /f %%g in ('dir "f:\temp\" /ad /b') do for /f %%a in ('dir "%%~fg" /ad /b') do icacls 

"%%~fg\%%a" /grant:r domain\mygroup:(OI)(CI)M 

Open in new window

0
 
LVL 1

Author Comment

by:cloughs
ID: 39804993
No that doesnt seem to work its still giving the command the start of the folder not the full folder name after the space.  Thanks for the quick response though
0
 
LVL 52

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39805061
In the code posted there is a blank line in the middle of the icacls command line, I don't think you want that.

In addition, I suspect the parens in the icacls parms are causing problems, since those are special characters in batch files.

Try something like this perhaps, although you may have to play around with quoting parts of the icacls parms:

for /f %%g in ('dir "f:\temp\" /ad /b') do (
  for /f %%a in ('dir "%%~fg" /ad /b') do (
    icacls "%%~fg\%%a" /grant:r "domain\mygroup:(OI)(CI)M"
  )
)

Open in new window

~bp
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 39805089
Just some thoughts:
- Adding an echo will show you what would happen (instead of giving you errors), like this:
 
echo icacls "%%~fg\%%a" /grant:r "domain\mygroup:(OI)(CI)M"

Open in new window

- Since you have a 'base' folder (Folder1), could you try icacls recursively instead:
cd Folder1
icacls * /grant:r "domain\mygroup:(OI)(CI)M" /T

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Vbscript 8 79
Event ID: 1202 / Source: SceCli 6 87
Windows Batch File - Read CSV 9 35
Need to know a list of new AD users that are created during the day. 5 45
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

26 Experts available now in Live!

Get 1:1 Help Now