Create a .bat file to run an exe as administrator - windows

I've created a batch file for running a setup.exe (code below) but I'm having issues getting the setup.exe to "run as administrator". I used this guide (shortcut trick) for aid.
start "" %CD%\Setup.exe
NOTE: My files will eventually be burnt to a DVD disk. They are currently in the directory "C:\Drivers\win8.1_x64\01a.chipset".
The batch file code runs the setup.exe (without admin privileges) fine, when running the batch file by itself (i.e. not running the shortcut).
However, when I run the batch file via the shortcut, Windows gives the error "Windows cannot find 'C:\Windows\system32\Setup.exe'".
The setup.exe directory is not in the system32 folder. Why does running the batch file find the setup.exe fine but not when I run it by the shortcut (so I can run the setup.exe as an admin)?

You can either set the working directory in the shortcut itself, or run Setup.exe not from working directory (%CD%) but from directory where script is located:
start "" "%~dp0\Setup.exe"
Difference from earlier answer is there's no need to cd to %~dp0. Just run the setup with full path.

By default admin privileged scripts are starting in C:\Windows\system32\
Try to put cd /d "%~dp0" at the beginning of your script which should change the work directory to the script's one.You can check this if you want to create a shortcut with admin permissions from the command line.

Related

Command prompt from C: drive, how to start executable in another drive?

I have a Windows machine with the current user in C:\Users\User.
I have an executable in another drive, let's say at D:\Folder\MyProg.exe.
Opening command prompt, it starts in the directory C:\Users\User
I type the command: start D:\Folder\MyProg.exe or D:\Folder\MyProg.exe
The exe fails to open, with a pop-up: MyProg has encountered an error
In order to run start the .exe from command prompt, I have to cd to the other directory and then start the exe.
Opening command prompt, it starts in the directory C:\Users\User
I type the command: cd /d D:\Folder && start MyProg.exe
The exe successfully opens.
Is there a better way to, from C:, start an executable in another drive?
Reproducing
Windows 10 Pro, v1809 (I don't think the version really matters)
My real use case is industrial automation, but one can observe the same result with convert.exe (cnet download link)
As commented by #Mofi, I realized the answer is most likely this:
But some programs are not good coded. Such programs depend on files in directory of the program and do not use appropriate code to reference those files from within the program with program files path, but use instead a relative path
As he instructed in the next comment, start provides a /d parameter that lets you specify a startup directory. Thus, a concise command would be:
start "" /d D:\Folder MyProg.exe
Note: the "" is for the <Title> field. The .exe I am opening is a GUI application (not a console application), so it is not necessary in this case, I just included in case other viewers find this useful in their application.
I have a Windows machine with the current user in C:\Users\User.
I have an executable in another drive, let's say at D:\Folder\MyProg.exe.
Opening command prompt, it starts in the directory C:\Users\User I type the command: start D:\Folder\MyProg.exe The exe fails to open.
In order to run start the .exe from command prompt, I have to cd to the other directory and then start the exe.
Maybe not. Try:
PATH D:\Folder;%Path%
"D:\Folder\MyProg.exe"

Executing a command-line .exe file

I have a .exe file converted from a .jar.
It is a command based application, so I have to start it with a batch script. Here is the batch script:
#echo off
cd C:\desktop\plant-text-adventure-win
start planttextadventure
pause
When I double click on the batch script, this happens: Windows could not find 'planttextadventure'. Please confirm if you have input the correct name and retry.
I don't know what is happening, I have no idea about cmd as I use Mac, but I can confirm I have an executable called planttextadventure.exe in a folder called plant-text-adventure-win.
You should test your batch file by executing it within a shell.
Simply enter within the start menu the command cmd to open up a shell. Within this black box you could now simply enter the commands from your batch script and lookout for some error message.
If you look at your script I would guess that the cd command (to change the current directory) is not correct. Maybe you should replace it with
cd %USERPROFILE%\Desktop\plant-text-adventure-win
because the desktop folder is on a default installation not directly under the root drive but within the user profile available.
Another solution to get this thing to work, is by opening the windows explorer, going to the .exe file you wish to execute and drag & drop the .exe file with a right mouse click onto the desktop.
Then a context menu appears and you select the option Create shortcut here.

Running a batch file through command prompt - system cannot find the path specified

I am trying to uninstall Oracle on this Windows 7 (64 bit) machine by downloading a standalone tool from Oracle, I need to run a batch file that is supposed to uninstall but I am unable to run it.
I tried to open command prompt as administrator and I am trying to run this as below:
As you can clearly see from the screenshot, I am doing a "dir" on the directory and can clearly see the file right there. Not sure what's going on here.
I also tried to run the batch file by double clicking from Windows Explorer and a terminal window opens and closes quickly but the batch file is not doing what it is supposed to do (it is clearly not executing from Windows Explorer).
Can anyone help me with this?
As theB pointed out above in a comment, this worked for me:
Open the bat file in notepad. I'll bet it starts with #echo off, and
that the error is actually coming from inside the batch file. The
error if the batch file itself wasn't found is 'X' is not recognized
as an internal or external command, operable program or batch file
'Run as Administrator' changes the current directory. See my answer here
Difference between "%~dp0" and ".\"?

Windows 7 Command Prompt: How do I execute a batch script from the command line?

I'm using Windows 7, and my problem is running this file from a console (cmd.exe):
W:\software\projects\myproject\build\msvc\build.bat
When I move into the folder containing the file manually and run it from there using the following command sequence, it works:
W:\>cd software
W:\software>cd projects
W:\software\projects>cd myproject
W:\software\projects\myproject>cd build
W:\software\projects\myproject\build>cd msvc
W:\software\projects\myproject\build\msvc>build.bat
However, when I try to run the file from the root directory in any of these ways:
W:\>software\projects\myproject\build\msvc\build.bat
W:\>call software\projects\myproject\build\msvc\build.bat
W:\>#call software\projects\myproject\build\msvc\build.bat
W:\>"software\projects\myproject\build\msvc\build.bat"
W:\>call "software\projects\myproject\build\msvc\build.bat"
W:\>#call "software\projects\myproject\build\msvc\build.bat"
I get the following error message:
The system cannot find the path specified.
I'm pretty sure you didn't have to navigate to the folder containing the file in order to run it when I was using Windows XP (though I could be wrong, of course), but this apparently seems to be the case with Windows 7. Or am I missing something?
You are correct. You do not need to navigate to the batch scripts folder before executing.
The error "The system cannot find the path specified." is most likely caused by something inside your batch-file.
Try to add
cd W:\software\projects\myproject\build\msvc
w:
or in a single command (as suggested by James K, Thanks!)
cd /d W:\software\projects\myproject\build\msvc
Searched a bit more and found this generic solution:
cd /d %~dp0
at the top of your batch file to set the working directory to the directory of the script to check whether this is the cause.
If you execute your file from W:\ this is where the commands are executed (working directory). It is most likely that your script cannot find some file it uses in this location.

How to register DLL from .bat file in Windows 7

As part of a poor-mans installation (on Windows 7) I need to register a DLL from a .bat file. I provide the user with a set of files that make up the application, tell them to copy them to some (any) directory, then, as the 1st part of the install, tell them to execute my register.bat file which invokes regsvr32 on the appropriate DLL(s)
This fails with 0x80004005 (permission) error. I then try running the .bat file as an Admin. This doesn't work as it opens the command prompt in \windows\system32 which is not where the DLLs to be registered are located. As I don't know where the user has placed the register.bat file I can't put the path to the DLL files in the .bat file.
Any thoughts?
Try using %~dp0 to get the folder the batch file is stored in, like:
regsvr32 %~dp0\mylibrary.dll
You can get and use the path of the current directory like this:
set "FullPath=%cd%\Test.dll"

Resources