I have a .bat that needs to run as admin although this creates problems with parts of the .bat when %userprofile% is used it will navigate to system32 rather than the userprofile that the .bat is run on.
I don't have any need to navigate to system32 with the .bat so if there was a way to correct this from the start of the .bat so following commands could be run as the currently logged in user.
You have two choices:
cd /d "%userprofile%"
or
pushd "%userprofile%"
I found the issue seems to be how the .bat is being used (through LogMeIn) for some reason it was going to system32 even using the pushd /d "%userprofile%" command I found a workaround that works with logmein is the following
pushd "%~dp0"
cd..
cd..
cd..
cd..
Related
I am currently creating an improvised installer for a cople software packages. To do this I have to install a couple MSI packages first before doing a couple file operations.
To install an MSI package I am using the following command:
start /wait msiexec /i "Myinstaller V2.1.msi" /qb
This command works and installs the package instantly and witout any problems via CMD.
But when I put this command in my batch file and execute it as an administrator, I get the following error:
This installation package could not be opened. Contact the application vendor to verify that this is a valid Windows Installer package
What cold be the problem? Using the same command via the console works flawlessly, only the batch file throws the error...
EDIT: I have also tried the /a parameter in order to install it as an administrator and it does not work either. Full command in batch file:
start /wait msiexec /qn /a "Myinstaller V2.1.msi"
EDIT2: I just realized that it only does not work when I start the batch file with Right click > Run as administrator
When I open a console with administrative rights and start my batch file it works for some reason...
Is there a way to make it work with the Right click > Run as administrator method?
SOLUTION: Thanks to RGuggisberg's answer I now know that the directory changes once the file is executed as an administrator. With a small change the installer gets fired up as an admin and works perfectly starting the installer from a relative path in the same directory:
#echo off
pushd %~dp0
start /wait msiexec /i "Myinstaller V2.1.msi" /qb
pause
I've now also implemented a feature to detect wether or not the installation fails or not:
#echo off
pushd %~dp0
start /wait msiexec /i "Myinstaller V2.1.msi" /qb
if %ERRORLEVEL% EQU 0 echo SUCCESSFULL
if NOT %ERRORLEVEL% EQU 0 echo MyProgram installation FAILED
pause
The current directory changes when you run as administrator. If you want to prove that to yourself, see this post
Difference between "%~dp0" and ".\"?
Include the full path to your filename and it will work.
So, there is always one installer (msi) in a folder for install generation, but the name changes many times. I tried doing the following:
"C:\Windows\system32\msiexec.exe" /i "C:\test\*.msi" /qn
But sadly, this doesn't work and complains. How can we get the name of the single msi in the folder, and plonk it into the command?
I'm using jenkins and using the "Execute windows batch command" item
Perhaps use a for loop to find all .msi files in that directory, and run it accordingly (obviously if there is more than one .msi you will need to tweak this logic):
for /r "C:\test" %%a in (*.msi) do msiexec /i "%%~dpnxa" /qn
I have a batch file that needs to copy my EXE to desktop and run it from there.
Code:
copy client.exe %USERPROFILE%\Desktop
%USERPROFILE%\Desktop\client.exe
What seems to happen is that client.exe is indeed copied to desktop and ran but acts as if it is in the directory of the original client.exe
The problem is that as far as the batch is concerned, the current directory is wherever the batch is execute from.
If you want the current directory to be the desktop, you'd need to explicitly set it
copy client.exe %USERPROFILE%\Desktop
pushd "%USERPROFILE%\Desktop"
client.exe
popd
or
copy client.exe %USERPROFILE%\Desktop
cd "%USERPROFILE%\Desktop"
client.exe
The first temporarily switches the current directory, so it is restored to what it was when run after client.exe terminates; the second makes a permanent switch to the desktop.
If you want start an application and specify where files will be saved (Working Directory), use either
CD /D "%USERPROFILE%\Desktop"
"full_path_to_app\client.exe"
or
START "" /D"%USERPROFILE%\Desktop" "full_path_to_app\client.exe"
Copying the .exe file seems to be a needless action.
I want to write simple Batch file will click on the batch file
I should go to my directory path should be D:\DS\Install
At present i am doing every 1hr go to
RUN command and typing to cmd and connect to D:\DS\Install
Instead of this I want short cut option.. :-)
If I understand your question correctly,
You want a shortcut on desktop which will open a command prompt and change directory to D:\DS\Install
This is pretty straight forward:
Go to C:\windows\system32 and copy cmd.exe to D:\DS\Install folder
Now right click on the cmd.exe in D:\DS\Install folder and send it to desktop create shortcut.
You will have a shortcut on desktop which will take you to the required folder everytime..
Just FYI... cmd.exe will always open from the directory where it is placed.
Just create a shortcut in your desktop, and when asked for the location, type this command
cmd.exe /k "cd /d "d:\ds\install""
This shortcut will call cmd to execute the cd to change the drive/folder and keep the window open to continue working with it.
You can also make a batch file called ds.bat and put it in c:\windows or any other folder that is on the path.
You can then use the WIN+R hotkey and type ds and enter.
#echo off
cd /d "d:\ds\install"
cmd /k
#echo off
cd /d "d:\ds\install"
cmd /k
I’m looking to create a batch file that transfers all my music from my external drive to my Samsung note 3. The idea is just to click the batch file and for it to update any song on my external on my phone.
Heres what I’ve got
XCOPY "E:\Music\*.*" "Computer\SAMSUNG-SM-N900A\Phone\Music\" /e /c /r /k /y /v
The problem I see is that it’s creating a folder on my desktop with that path, the directory on my desktop being computer with the following subfolders.
I’m not sure why it won’t recognize the path, any ideas?
You can't directly copy files from windows to an android device, its a different file system. You can probably do it manually with this program:
https://android-file-manager.en.softonic.com/