Question

I have a batch file which looks like this:

set OWNPATH = %~dp0

for /r %OWNPATH% %%F in (*.ocx) do ( echo %%F )

It correctly lists all OCX files in the same folder when I start it, but lists the OCX files in

C:\Windows\System32

when I right-click it and select "Run as administrator".

How can I fix that? The script generally needs admin rights.

Was it helpful?

Solution

Your set is setting %OWNPATH % (note the space between OWNPATH and the =). Therefore %OWNPATH% (with no space) is not defined, and for /r %OWNPATH% %%F ... gets expanded to for /r %%F, and the for loop ends up looking in the current directory (which is C:\Windows\System32 when run as administrator).

Get in the habit of doing your assignments like this to avoid that common mistake:

set "OWNPATH=%~dp0"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top