We help IT Professionals succeed at work.

if then inside for loop not working - Batch scripting

Antonio King
Antonio King asked
on
138 Views
Last Modified: 2017-04-04
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

Comment
Watch Question

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
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
Antonio KingIT Manager

Author

Commented:
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

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

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

~bp
Test your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Antonio KingIT Manager

Author

Commented:
Hi bp
Does this help?
It's a screenshot of the log file that gets created when the script is run
screenshot of log.log
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
See my last comment (before this one) about the THEN in the IF...

~bp
Antonio KingIT Manager

Author

Commented:
Thanks! What a rookie error!
Sometimes a fresh pair of eyes is needed.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Welcome, glad that helped.

~bp

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.