Solved

How to cope with folders with spaces in a batch file

Posted on 2014-01-23
4
593 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 51

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

9 Experts available now in Live!

Get 1:1 Help Now