Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to cope with folders with spaces in a batch file

Posted on 2014-01-23
4
603 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 143

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 53

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 38

Expert Comment

by:Gerwin Jansen, EE MVE
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Scenerio: You have a server running Server 2003 and have applied a retail pack of Terminal Server Licenses.  You want to change servers or your server has crashed and you need to reapply the Terminal Server Licenses. When you enter the 16-digit lic…
On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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