Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to cope with folders with spaces in a batch file

Posted on 2014-01-23
4
Medium Priority
?
627 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
[X]
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
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 57

Accepted Solution

by:
Bill Prew earned 1500 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Setting up a Microsoft WSUS update system is free relatively speaking if you have hard disk space and processor capacity.   However, WSUS can be a blessing and a curse. For example, there is nothing worse than approving updates and they just have…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

715 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