Solved

Access denied error when accessing directory

Posted on 2013-05-23
9
356 Views
Last Modified: 2013-07-04
I am using the following to iterate over files in a directory...

SET ROOT=C:\
SET VERSION=1

(FOR %%F IN (%ROOT%\%VERSION%\*.*) DO (
 ECHO %%F
)

When I run this code I get an Access Denied error message, but if I replace '%ROOT%\%VERSION%\*.*' with 'C:\1\*.*' then things work. It is something about the variable substitution I guess but I cant figure out what? Any ideas?
0
Comment
Question by:Blowfelt82
  • 4
  • 3
  • 2
9 Comments
 
LVL 52

Expert Comment

by:Bill Prew
ID: 39190610
You may have some spaces at the end of the set lines, check that.  You can protect against that by changing the lines to this also:

SET "ROOT=C:\"
SET "VERSION=1"

~bp
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 39190611
Also, does it echo any files names before the error?

~bp
0
 
LVL 83

Expert Comment

by:oBdA
ID: 39190628
Two major errors:
1. The opening bracket in front of "FOR" should not be there.
2. The difference between the two versions is that when you're using the variables, "ROOT" ends with a backslash, and you have a backslash as well between "%ROOT%" and "%VERSION%" in the FOR loop, so with the variables, you end up with "C:\\1\*.*".
Try
SET ROOT=C:
SET VERSION=1
FOR %%F IN (%ROOT%\%VERSION%\*.*) DO (
 ECHO %%F
)

Open in new window

0
 

Author Comment

by:Blowfelt82
ID: 39190636
Double checked and there are no spaces after the variable declarations, tried your declaration approach too but had the same effect. I echoed '%ROOT%\%VERSION%\*.*' to screen and the output was as expected and the file existed?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Blowfelt82
ID: 39190642
Sorry the extra '(' was a typo on my part so please ignore. Edited the ROOT variable as described and had the same problem - when I output the string it is a valid path which relates to a directory path that does exist. If I take the substituted variable and replace in the code it works as expected?
0
 
LVL 83

Expert Comment

by:oBdA
ID: 39190693
Please open a new command prompt, copy and paste (right-click the title bar) the text below (you can do that all at once) into this command prompt, and post the complete output generated:
SET ROOT=C:
SET VERSION=1
FOR %%F IN (%ROOT%\%VERSION%\*.*) DO (ECHO %%F)

Open in new window

0
 

Author Comment

by:Blowfelt82
ID: 39190908
Followed your instructions and the directory contents were echoed out as expected! But when I run this as part of my existing script I get the same issue? This must be something obvious I am doing - I will try and figure this out!
0
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 39190931
Check the current script in a hex editor (for example http://mh-nexus.de/en/hxd/) to see whether there are control characters embedded someplace. Stranger things have happened.
Have you tried running the script in a fresh command prompt as well?
0
 

Author Closing Comment

by:Blowfelt82
ID: 39299565
Yep, looks like some dodgy characters existed! Problem solved.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 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

26 Experts available now in Live!

Get 1:1 Help Now