Running batch files as administrator change working directory
I need a bit help with Batch files on the new Vista OS. I know if I start a batch file from Explorer the working directory is the directory which holds the file. And if I start a batch file from a shortcut then I can specify a different one, right? But here is the problem. When I start a batch file as Administrator the working directory is always C:\Windows\System32, even if the file is in a dissimilar directory or starts from a shortcut whit an explicit working directory. Due to this all my batch files get fails with all another executable (batch, exe, ecc.) in case not using absolute paths.
As far as I know the best method to edit the batch file is by changing the current directory. For example like this
CD \my_directory
CALL another_batch_file
Am I right up to here? If yes, then I would like to know if there is reason because the working directory is not respected or is simply a missing functionality. Any idea?
Re: Running batch files as administrator change working directory
Code:
@echo off
for %%F in (folder1 folder2 folder3) DO xcopy /h /r /k /x /y /S /E "\\servername\folder" "\\servername\%%F"
exit
This will look at the all the folder names 'folder1, folder2, and folder3' and will copy all files from the origin to them
Re: Running batch files as administrator change working directory
In XP, %~dp0 should give you the path of the batch file. I don't have a box nearby to test Vista.
http://www.microsoft.com/resources/d....mspx?mfr=true
Re: Running batch files as administrator change working directory
The OP are not the only people looking for the answers to this issue on the internets. Moron.
Re: Running batch files as administrator change working directory
Thank you very much for all your helps guys. But still i have a bit problem, actually a small confusion. I request you to please tell me how exactly i can create the batch file that will copy one file to multiple drives. Also please let me know how to do this with a file that is password protected. Many thanks.
Re: Running batch files as administrator change working directory
just put this as the first line:
chdir %~dp0
works in xp, vista, and win7
Re: Running batch files as administrator change working directory
Quote:
Originally Posted by
CapinWinky
just put this as the first line:
chdir %~dp0
works in xp, vista, and win7
This unfortunately doesn't wok if the batch file was located on a different drive.
(e.g. on drive D: - chdir changes directory but not the drive letter)
Re: Running batch files as administrator change working directory
you need to use
chdir /D %~dp0
to be sure that also the drive letter is correct