Solved

Access denied error when accessing directory

Posted on 2013-05-23
9
360 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
[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
  • 3
  • 2
9 Comments
 
LVL 56

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 56

Expert Comment

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

~bp
0
 
LVL 85

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
Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

 

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 85

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 85

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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

628 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