Solved

Access denied error when accessing directory

Posted on 2013-05-23
9
348 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 51

Expert Comment

by:Bill Prew
Comment Utility
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 51

Expert Comment

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

~bp
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Blowfelt82
Comment Utility
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 82

Expert Comment

by:oBdA
Comment Utility
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
Comment Utility
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 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
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
Comment Utility
Yep, looks like some dodgy characters existed! Problem solved.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

7 Experts available now in Live!

Get 1:1 Help Now