Open a .BAT file in another directory from within a .BAT file - windows

I have a .BAT file on my desktop that calls another .BAT file in the same location named MySQL:
start MySQL
pretty complicated right? How could I access this same file if it was in c:\ProgramFiles? I know I need to first go up one directory, but I cannot for the life of me make it work. I'm using windows 10 by the way.

You should do like this example :
#echo off
CD /D "%Programfiles%\Mozilla FireFox\"
Start "" Firefox.exe
and in your case it should be like this :
#echo off
CD /D "%Programfiles%\Sqlfolder\"
Start "" Mysql.exe

Related

How to run a .bat (that runs an .exe that is in the same directory) as administrator from a pendrive?

I'm able to run a .bat (that runs an .exe that is in the same directory) as administrator: I right-click in the bat file and select "Run as administrator".
To be able to do that, I used the following answer: Run exe from current directory in batch
Here's the code:
#echo off
:A
cls
echo This will start the program.
pause
cd %~dp0
start %1myprogram.exe
exit
However, this will only work if the .bat file and the program are in the system drive.
Because if they are, for instance, in a pendrive, and I right-click and select "Run as Administrator", I get the error:
"Windows cannot find 'myprogram.exe'. Make sure you've typed the name correctly, then try again."
Why this happens and how can I fix it?
I thought that by using cd %~dp0 it would always point to the folder in which the bat .file resides.
Thanks in advance.
Solution
Change cd %~dp0 to cd /d %~dp0
Explanation
When you run something with administrator privileges, the working directory changes to:
'C:\Windows\System32'
Although %~dp0 still points to the drive and the directory containing the batch file, cd %~dp0 does not work, because it only changes the directory, but stays on the same drive.
Using the /d parameter, you can tell the cd-command to change the drive, too.
You may need to tell cd to also change drives:
cd /d %~dp0
If the current drive is C: (e.g., the prompt says C:\>), and you do CD D:\FOO, the current directory on drive D: is set to \FOO, but you will still be "on" drive C:. Try the following:
#echo off
:A
cls
echo This will start the program.
pause
cd %~dp0
%~d0
start %1myprogram.exe
exit
(also, why %1myprogram.exe instead of just myprogram.exe, or even just myprogram? If you're right-clicking on the batch file to run it, there isn't going to be a %1.)

Why cannot i use batch XCOPY while running in admin mode?

i have run very simple script:
xcopy some.exe c:\folder\ /h/y and it works normally. but when i try to run .bat file with this code as administrator - cmd line opens for a moment but nothing happens (file not copied). Can anyone explain this issue?
i also tried to use echo xcopy instead of xcopy, but nothing had changed.
i need only admin running of .bat file, cause i want to copy file in \windows\system32 folder
when you start a batchfile as administrator, it's working directory is C:\windows\system32\. So your script doesn't find your file. Either work with absolute paths, or change the working directory.
You can change it to the directory, where your batchfile resides with:
cd /d "%~dp0"
Note: to keep the window open to read any errormessages, append a pause command.

CMD script that goes to a folder

I'm trying to make a CMD script that will do CD Desktop\Crunchyroll\pyscripts and enter it, then I want to be able to type a command. Is there a way I can make a script to start in the Desktop\Crunchyroll\pyscripts directory on my computer?
Put this into a blank text file called MyScript.CMD
Double click it.
#echo off
CMD /K CD /D "%userprofile%\Desktop\Crunchyroll\pyscripts"
To create a symbolic link on Windows, open Command Prompt (as an Administrator). Let's say your username is Jason. And the symlink we will name 'code'.
cd \Users\Jason
mklink /D code Desktop\Crunchyroll\pyscripts
cd code
dir
All your files in pyscripts will be listed. You basically have created a shortcut to your pyscripts.
Personally, I'd simply create a shortcut to cmd.exe and set Properties/shortcut/Startin to Desktop\Crunchyroll\pyscripts.
If you really want to jump to pyscripts from within an existing cmd instance, then create a textfile named 'pyscripts.bat' containing
#echo off
CD "%userprofile%\Desktop\Crunchyroll\pyscripts"
and store it somewhere in your PATH.
Then executing pyscripts from within a cmd instance will jump to the indicated directory and
....
PUSHD
call pyscripts
{do some python}
POPD
...
should allow you to execute python from that directory within a batch file.

Run a script file as administrator

I have this .vbs script that I am trying to run on windows 7.
It has to run with full permissions and it has to do it automatically.
To be clear When the user double clicks on the file it will get the prompt that asks "to allow to run the file as an administrator", and then run with full permissions.
To do this I created a batch file (run.bat) that calls the script file
cscript "V02.vbs"
pause
then I created a shortcut for the batch file which I can choose to run as admin.
The problem I encounter now is that when I run the batch file as admin the folder changes to c\windows\system32. The script, batch file and shortcut are all in the same folder. is there a way to get the folder location?
I may have misunderstood...
The easiest solution would be to "hardcode" that path into the patch file with a -
cd \path\to\my\script
cscript myscript.vbs
pause
Use "modifiers with batch parameters". From the linked documentation:
%~dp1 Expands %1 to a drive letter and path.
Since %0 is your script, %~dp0 is what you want:
cscript "%~dp0V02.vbs"
pause
Note that %~dp0 includes the trailing backslash, so your v02.vbs file name above becomes quite hard to read.

open command prompt window and change current working directory

I'm terribly new to scripting on windows. Using windows 7 64.
I'm trying to make a .bat file that I can double click, and have it open a command prompt and automatically cd me to a certain directory.
I tried making a .bat file with
#ECHO OFF
cmd "cd C:\my\destination"
Which opens what looks like a command prompt, but doesn't seem to let me type any commands.
I then tried:
#ECHO OFF
start cmd "cd C:\my\destination"
But this just sent me into a loop opening tons and tons of prompts until my computer crashed :) The .bat file was located in the destination directory if that matters.
This works for me:
#ECHO OFF
cmd.exe /K "cd C:\my\destination && C:"
The quoted string is actually two commands (separated by a double ampersand): The first command is to change to the specified directory, the second command is to change to the specified drive letter.
Put this in a batch (.BAT) file and when you execute it you should see a Command Prompt window at the specified directory.
Use the /K switch:
#ECHO OFF
start cmd.exe /K "cd C:\my\destination"
But IMHO, the most useful switch is /?.
Starts a new instance of the Windows XP command interpreter
CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
[[/S] [/C | /K] string]
/C Carries out the command specified by string and then terminates
/K Carries out the command specified by string but remains
/S Modifies the treatment of string after /C or /K (see below)
/Q Turns echo off
...
And only if it does not work, then Google it, as #Neeraj suggested :D
This could be done like that:
#ECHO OFF
cd /D "C:\my\destination"
cmd.exe
If you need to execute a file or command after you open the cmd you can just replace the last line with:
cmd.exe /k myCommand
#ECHO OFF
%comspec% /K "cd /D d:\somefolder"
The /D will change folder and drive and works on 2000+ (Not sure about NT4)
If you take a look at Vista's open command here, it uses cmd.exe /s /k pushd \"%V\" but I don't think %V is documented. Using pushd is a good idea if your path is UNC (\\server\share\folder) To get UNC current directory working, you might have to set the DisableUNCCheck registry entry...
Why so complicated? Just create an alias to cmd.exe, right click on the alias and navigate to its settings. Change the "execute in" to the path you want to have as standard path. It will always start in this path.
just open a text editor and type
start cmd.exe
cd C:\desired path
Then save it as a .bat file. Works for me.
You can create a batch file "go-to-folder.bat" with the following statements:
rem changes the current directory
cd "C:\my\destination"
rem changes the drive if necessary
c:
rem runs CMD
cmd

Resources