Solved

How to cope with folders with spaces in a batch file

Posted on 2014-01-23
4
604 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 54

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Auditing domain account logon attempt, failure, lockout 2 164
ticket bloat 3 70
Domain Controller FSMO 7 73
Drive mapping problem 7 45
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
A quick step-by-step overview of installing and configuring Carbonite Server Backup.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 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