I have a requirement where I need to run a batch script which would compile a custom module and then come back to the project's root location and run the project.
For this, I have written something like below
SET curDir=%~dp0
PUSHD %curDir%
echo %curDir%
cd modules\custom-module
yarn build
POPD
echo %CD%
yarn start
but after the yarn build command, it stays in the modules\custom-module directory.
Before changing the directory call 'setlocal'. So your script will look like following
cd <- prints current dir
setlocal
cd modules\custom-module
yarn build <- if yarn is script, do 'call yarn build' instead
endlocal
cd
yarn start
Related
I created a npmruns.bat file with a content:
C:\myfolder>npm run s
I wanted to create a script file which run a command: npm run s under specified location: C:\myfolder , but it doesn't work. I run it by double click (I have an admin rights).
I tried to create a script which can be executed from any location (other than C:\myfolder).
pushd and cd /d will be the options for this:
#echo off
pushd "C:\myfolder"
call npm.cmd run s
popd
This will push to the path of the batch-file as the working directory, popd is not required if you do not need to go back to the starting working directory, which as admin, will be "%systemroot%\system32"
Alternatively you can run:
cd /d "c:\myfolder"
I'm writing a batch file that runs a command via git-cmd.exe but it doesn't run the command(s) after it.
I've tried to use CALL, START /WAIT, and START /B /WAIT. All have the same behavior. Maybe there is a parameter should be sent to git-cmd.exe to execute the command and exit but I didn't find any guide explaining how to use git-cmd.exe.
This is a sample batch file:
#ECHO OFF
SET "PATH=C:\Ruby26-x64\bin;C:\Program Files\nodejs;%PATH%"
SET "CurrentDirectory=%CD%"
CD /D "%UserProfile%\AppData\Local\GitHub\PortableGit_*\"
SET "GitDirectory=%CD%"
CD /D "%CurrentDirectory%"
"%GitDirectory%/git-cmd.exe" CALL rake build
PAUSE
The command passed to git-cmd.exe is executed but the PAUSE command doesn't execute until I type EXIT command manually in the 'Command Prompt' window.
I've also tried a simple DIR command instead of rake build but the same issue still occurs:
"%GitDirectory%/git-cmd.exe" DIR
PAUSE
Thanks to the suggestions, the issue was resolved by passing EXIT command to git-cmd.exe as follows:
#ECHO OFF
SET "PATH=C:\Ruby26-x64\bin;C:\Program Files\nodejs;%PATH%"
SET "CurrentDirectory=%CD%"
CD /D "%UserProfile%\AppData\Local\GitHub\PortableGit_*\"
SET "GitDirectory=%CD%"
CD /D "%CurrentDirectory%"
"%GitDirectory%/git-cmd.exe" "CALL rake build & EXIT"
PAUSE
There was a useful conversation between me and someone else (I think his name was mony) but it was deleted, don't know why?!
He sent me this link with the difference between & and && between commands:
https://stackoverflow.com/a/25344009/9586127
The & before the EXIT command in order to execute both commands independent on result of the first one. If && is used, the EXIT command will be executed only if the first command succeeded (exit code 0).
In addition, he told me a way to replace the lines 3:6 by one line to be:
FOR /D %%I IN ("%LocalAppData%\GitHub\PortableGit_*") DO SET "GitDirectory=%%I"
Following on from an answer to this question
For the Windows command prompt I can define aliases as follows:
#echo off
DOSKEY ns=npm start
DOSKEY nsr=npm run serve
I want to define an alias that will combine these two commands. I tried the following:
#echo off
DOSKEY nss=npm start && npm run serve
When I try to open the command prompt the window will open but the > prompt does not appear.
I think that the inclusion of && is causing a problem.
The command separator for DOSKEY is $T
For your example:
DOSKEY nss=npm start $T npm run serve
I looked at an answer to a question on superuser. The following approach resolved my problem:
#echo off
DOSKEY nss=npm start ^&^& npm run serve
My approach is to load the macros from a text file:
a.bat:
#doskey /macrofile=C:\%HOMEPATH%\bin\aliases.txt
and in the macro file, && works:
aliases.txt:
cl=cd /d $* && dir
Then if I type "cl bin" from HOMEPATH, it does "cd /d bin" and then "dir":
C:\Users\mike> cl bin
09/17/2020 09:27 AM 1,303 a.bat
09/30/2020 03:17 PM 886 aliases.txt
Try writing it as a batch file and then calling the batch file use the DOSKEY command
REM do_npm.bat
npm start
npm run serve
Then, run the DOSKEY command
DOSKEY npm=do_npm.bat
See if that works for you
Here are the commands in batch:
D:
cd D:\Startup\venv\Scripts
activate
cd D:\Startup\
python manage.py runserver
Commands after "activate" are not executed for some reasons. I tried to put "cmd /k activate" instead "activate" but result is still the same except the command line is still open. What is wrong here
I suppose activate is a batch file and therefore needed is:
cd /D D:\Startup\venv\Scripts
call activate.bat
cd D:\Startup
python.exe manage.py runserver
Without command call the processing of a batch file continues on other batch file without returning which is the reason why the last two lines where never executed. Run in a command prompt window call /? and read output help and for more details see for example also answer on How to call a batch file in the parent folder of current batch file?
Command CD with parameter /D makes it possible to change directory and drive at the same time as cd /? executed in a command prompt window explains.
In batch files it is advisable to specify batch files and executables with file extension.
I am using maven in a project here at work and I've come up to a rather strange (at least for me) problem. When I do an: mvn package everything seems ok but the output of mvn disappears as soon as maven completes. To test it more I just did a: mvn --help and I could not see the output. As soon as the command finishes the prompt is cleared. I am doing my work now using redirections: mvn package > out and then: type out in order to see the output. Any help would be greatly appreciated!
Thanx!
Solved: To whoever finds this useful. At the end of mvn.bat there was the line
cmd /C exit /B %ERROR_CODE%
which I had to comment out like this:
#REM cmd /C exit /B %ERROR_CODE%
Check if the MAVEN_TERMINATE_CMD is set to on. If it is unset it:
set MAVEN_TERMINATE_CMD=
Check that the mvn.bat script in maven's bin directory doesn't contain an exit command without the /b option. It should end with the following line:
cmd /C exit /B %ERROR_CODE%
If neither alternative solves your problem, set the MAVEN_BATCH_PAUSE variable to on:
set MAVEN_BATCH_PAUSE=on
before you run maven. This should cause mvn.bat to wait for a keystroke before exiting.
The following is an example of how to use mvn in a batch file under Windows 7 and Maven 3.2.1
:: ensure variables do not propagate outside this batch call
setlocal
:: save current directory and change to ".."
pushd ..
:: tell mvn.bat to exit after done, not to spawn cmd (why is this by default..)
set MAVEN_TERMINATE_CMD=on
:: spawn new cmd prompt to handle the mvn call, which blocks and exits
cmd /c mvn clean install
:: do something fancy here
:: cd to saved directory (pushd)
popd
:: pause the script and require pressing a key to continue
pause