Link to home
Start Free TrialLog in
Avatar of Antonio King
Antonio King

asked on

if then inside for loop not working - Batch scripting

Hi
I have created a batch script that adjusts folder ownership and permissions. The script recursively cycles through all subfolders names of a folder. The names of the folders are usernames. The script assigns folder ownership and full permissions to the that user. Ie. If the folder name is "JBloggs", then Jbloggs will be made the owner and be granted full permission.

There is one folder I do not want the script to touch. This folder is called "default"

I've added an IF then NEQ statement so if it encounters a folder with the name "default" it will just skip over it, but it doesn't seem to be working.

Any advice would be greatly appreciated!

@ECHO OFF
SETLOCAL enabledelayedexpansion

CD /D "D:\Public\Users\"
FOR /D %%A IN (*) DO (
	IF %%A NEQ "default" THEN (
REM		TAKEOWN /F "%%A" /A /R /D Y
REM		ICACLS "%%A" /RESET /T
REM		ICACLS "%%A" /GRANT:R "DOMAIN\%%A:(OI)(CI)F"
REM		ICACLS "%%A" /SETOWNER "DOMAIN\%%A" /T
		ECHO %%A >> C:\Public\Scripts\Log.txt
	)
)
ENDLOCAL
EXIT

Open in new window

Avatar of Bill Prew
Bill Prew

Try this adjustment.  The /i will make the comparison not case sensitive, and the double quotes are needed around the loop variable %%A.

IF /i "%%A" NEQ "default" THEN (

Open in new window

~bp
Avatar of Antonio King

ASKER

Hi Bp
Thanks for taking the time to look at this for me.
IO've made your recommended adjustments, however the name "default" is still appearing in the log.log file suggesting the changes have not had the desired affect.

To confirm, this is now the script I'm using...
@ECHO OFF
SETLOCAL enabledelayedexpansion
CD /D "D:\Public\Users\"
ECHO The following folders permissions have been changed: > "C:\Public\Scripts\Folder permissions\log.log"
FOR /D %%A IN (*) DO (
	IF /i "%%A" NEQ "default" THEN (
		REM TAKEOWN /F "%%A" /A /R /D Y
		REM ICACLS "%%A" /RESET /T
		REM ICACLS "%%A" /GRANT:R "DOMAIN\%%A:(OI)(CI)F"
		REM ICACLS "%%A" /SETOWNER "DOMAIN\%%A" /T
		ECHO %%A >> "C:\Public\Scripts\Folder permissions\log.log"
	)
)
ENDLOCAL
EXIT

Open in new window

Can you post the log file here?  If not, please post the exact line(s) that have DEFAULT in them.

~bp
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi bp
Does this help?
It's a screenshot of the log file that gets created when the script is run
User generated image
See my last comment (before this one) about the THEN in the IF...

~bp
Thanks! What a rookie error!
Sometimes a fresh pair of eyes is needed.
Welcome, glad that helped.

~bp