1.

Solve : Vista copy command failure?

Answer»

Vista Home edition:
I WROTE a simple batch file as follows to copy a group of dll & ocx files from a USB drive to my hard drive, and then register them individually.

Code: [Select]copy "\MiscDLLs\*.*" "C:\MyProgram\"

regsvr32.exe "C:\MyProgram\ccrpFD.ocx"
regsvr32.exe "C:\MyProgram\comct232.ocx"
regsvr32.exe "C:\MyProgram\msvbvm50.dll"
regsvr32.exe "C:\MyProgram\richtx32.ocx"

When I run the batch file with the routine double-click, the copy command is successful, but ... the four regsvr32.exe commands fail.
So ... I tried Right-click - "Run as Administrator". This time the four files were each registered successfully. (Note that the four files had already been copied to the destination folder.)

But, here's the weird part, I WANTED to test the batch file again from the beginning so I deleted the four files from MyProgram and tested the batch file again. It failed!

After repeated testing, I discovered that when the batch file was "Run as Administrator" the copy command failed and then the four regsvr32.exe commands would also fail because the four files were not available to be registered. Yet, when run as normal with the double-click the copy command worked every time. (but ... the four regsvr32.exe commands fail.)

So here's the question: "Why does the copy command fail when "Run as Administrator" is used?
I assume that there must be a syntax error involed, but OK, I may have partially figured out the answer.

In the copy cmd: Code: [Select]copy "\MiscDLLs\*.*" "C:\MyProgram\"I intentionally left out the drive letter of the USB drive because the drive letter will vary from one machine to another. Normally, "DOS"(or Windows CMD) looks on the current drive and fills in the current drive letter completing the path for me. (This works every time when the batch is run normally.)

The error that I am receiving is: Quote

The SYSTEM cannot find the path specified.
That indicates to me that when the batch is "Run as Administrator" it somehow LOSES it's ability to see the current drive (the USB drive), it then is unable to find the path.

Is there any way to avoid this when running a batch file "as Administrator"?
Or is there a way to use an environment variable that would specify the correct drive letter of the USB drive for me?I think "Run as administrator" changes the current directory. If you put the line...

echo %cd%

... at the start of your batch you will see what it reports when run as ordinary user and as administrator.

However there is a way around this - every batch file knows its own drive letter, path and name and some more stuff besides.

The variable %0 holds the information and you can use the standard  d,n,p,x, etc variable modifiers to extract it. You put a tilde (~) and a letter after the percent sign. For example %~d0 holds the drive letter and colon. You can use upper or lower case letters but the convention is to use lower case.

So you could put

Code: [Select]copy "%~d0\MiscDLLs\*.*" "C:\MyProgram\"
Note that the directory, path, etc obtained this way refer to the actual drive that the batch is stored on and do not change, and are independent from the current directory that the batch is being run from, so I expect that this will be suitable for your purposes.

This is IDme.bat. It is on a pendrive which has the letter G, in the root directory...

Code: [Select]echo off
echo Drive letter         %~d0
echo Folder               %~p0
echo Name                 %~n0
echo Extension            %~x0
echo Drive\path\name      %~dpnx0
echo fully qualified path %~f0
echo File date            %~t0         
echo File size in bytes   %~z0
pause

I double click on it in Explorer and I see this in the command window that opens...


Code: [Select]Drive letter         G:
Folder               \
Name                 IDme
Extension            .bat
Drive\path\name      G:\IDme.bat
fully qualified path G:\IDme.bat
File date            18/10/2010 06:25 PM
File size in bytes   291
Press any key to continue . . .

The variable modifiers also work on parameters %1 to %9 passed from the command line, as well as the FOR variables %%a - %%z and %%A - %%Z, and you can see a list in the FOR documentation obtained by tyoing FOR /? at the prompt. The date is shown according to your local settings.





 I'm sure that will work. It's exactly what I was looking for!

Thanks!

This is definately a keeper. I've already thought of a few other applications for the information you provided. Thanks again!


Discussion

No Comment Found