Solved

Access denied error when accessing directory

Posted on 2013-05-23
9
358 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 53

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 53

Expert Comment

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

~bp
0
 
LVL 84

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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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
 

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 84

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 84

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

830 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