Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 363
  • Last Modified:

Access denied error when accessing directory

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
Blowfelt82
Asked:
Blowfelt82
  • 4
  • 3
  • 2
1 Solution
 
Bill PrewCommented:
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
 
Bill PrewCommented:
Also, does it echo any files names before the error?

~bp
0
 
oBdACommented:
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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
Blowfelt82Author Commented:
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
 
Blowfelt82Author Commented:
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
 
oBdACommented:
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
 
Blowfelt82Author Commented:
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
 
oBdACommented:
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
 
Blowfelt82Author Commented:
Yep, looks like some dodgy characters existed! Problem solved.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now