The following batch script is given:
set JAVA_HOME="%cd%\tools\java\windows\jdk"
set PATH="C:\Windows\System32";"%cd%\tools\maven\bin";"%cd%\tools\nodejs"
cd apps\build\nodejs && npm install gulp
%cd%\tools\java\windows\jdk\bin\java.exe -jar tool.jar
Now this command
%cd%\tools\java\windows\jdk\bin\java.exe -jar tool.jar will be
no longer executed after this
cd apps\build\nodejs && npm install gulp
is executed. Actually the command prompt just closed after execution.
What is the reason for this?
My npm.cmd (i can't edit this file)
:: Created by npm, please don't edit manually.
#ECHO OFF
SETLOCAL
SET "NODE_EXE=%~dp0\node.exe"
IF NOT EXIST "%NODE_EXE%" (
SET "NODE_EXE=node"
)
SET "NPM_CLI_JS=%~dp0\node_modules\npm\bin\npm-cli.js"
FOR /F "delims=" %%F IN ('CALL "%NODE_EXE%" "%NPM_CLI_JS%" prefix -g') DO (
SET "NPM_PREFIX_NPM_CLI_JS=%%F\node_modules\npm\bin\npm-cli.js"
)
IF EXIST "%NPM_PREFIX_NPM_CLI_JS%" (
SET "NPM_CLI_JS=%NPM_PREFIX_NPM_CLI_JS%"
)
"%NODE_EXE%" "%NPM_CLI_JS%" %*
Related
I'm trying to feed the file name dynamically to a command through windows batch script. Here's my code,
#echo off
set /p path="Enter the project path: "
for /R "%path%" %%I in (*.java) do (java -classpath MyCheckStyle.jar;checkstyle-5.6-all.jar com.puppycrawl.tools.checkstyle.Main -c custom_check.xml %%I)
pause
Basically, I want to get all the .java files from any given directory and feed those java files to another command which will run for each file. In place of %%I I am trying to provide the java file dynamically. But it says java command not recognized.
If I use java -classpath MyCheckStyle.jar;checkstyle-5.6-all.jar com.puppycrawl.tools.checkstyle.Main -c custom_check.xml path/filename.java alone in a batch file. It works fine.
I have tried the following also,
#echo off
set i=-1
set /p path="Enter the project path: "
for /R "%path%" %%I in (*.java) do (
set /a i=!i!+1
set filenames[!i!]=%%I
)
set lastindex=!i!
for /L %%f in (0,1,!lastindex!) do (
java -classpath MyCheckStyle.jar;checkstyle-5.6-all.jar com.puppycrawl.tools.checkstyle.Main -c custom_check.xml !names[%%f]!
)
pause
But again it says java command not recognized.
What am doing wrong here? Why I can not pass the .java file names dynamically?
Your named your variable with project path "path", and it's bad, because as soon as you do
set /p path="Enter the project path: "
you overwrite the system "path" and then Windows cannot find your "java.exe".
Rename it to project_path:
set /p project_path="Enter the project path: "
for /R "%project_path%" %%I in .......
For example, I have a simple batch script get_path.bat
#echo off
echo C:\Software\dt
Also, I have another simple batch script switch_dir.bat
#echo off
get_path.bat > target.tmp
set /p TARGET=<target.tmp
cd %TARGET%
Now, what I want to accomplish is that when I invoke in cmd.exe the batch file switch_dir.bat, my current working directory changes to C:\Software\dt.
As of now, the scripts work but they run in a process spawned from cmd.exe so my current working directory stays the same. What is missing? Basically, we need Unix-like source or . here.
Well, there are many possible solutions:
Start your script with cmd /c:
All you have to write in cmd is:
cmd /c switch_dir.bat
Using popd/pushd in your batch file:
In your switch_dir.bat add:
#echo off
pushd dir\you\want\to\remain\
get_path.bat > target.tmp
set /p TARGET=<target.tmp
cd %TARGET%
rem [code...]
popd
An additional note: A better way to find directory specified in get_path.bat is to use a for /f loop like this:
#echo off
pushd dir\you\want\to\remain\
for /f "delims=" %%A IN ('get_path.bat') do set TARGET=%%A
cd %TARGET%
rem [code...]
popd
Recently installed Anaconda (1.9) for my python project on win7
After installation, I built a python 3 support environment with instruction in this page. My next task is to activate my python environment automatically with built-in batch file.
I used the command in [Anaconda Command Prompt] shortcut I found in my start menu. It runs a batch-file called [anaconda.bat]
After observing the batch file I realized it seems to be capable of taking an input argument that is supposed to be the environment I would like to activate. So I copied the shortcut and modified it as
C:\Windows\System32\cmd.exe /k "C:\Anaconda\Scripts\anaconda.bat py3k"
Then I double clicked on the new shortcut, it opened a new command window but...the designated environment did not activate!
#echo off
rem +===========================================================================
rem | Initialisation
rem +===========================================================================
verify bogus-argument 2>nul
setlocal enableextensions enabledelayedexpansion
if ERRORLEVEL 1 (
echo error: unable to enable command extensions
goto :eof
)
for %%i in ("%~dp0..\envs") do (
set ANACONDA_ENVS=%%~fi
)
if not "%1" == "" (
if not exist "%ANACONDA_ENVS%\%1\python.exe" (
echo No environment named "%1" exists in %ANACONDA_ENVS%
goto :eof
)
set ANACONDA_ENV_NAME=%1
set ANACONDA=%ANACONDA_ENVS%\%1
title Anaconda (%ANACONDA_ENV_NAME%^)
) else (
set ANACONDA_ENV_NAME=
for %%i in ("%~dp0..") do (
set ANACONDA=%%~fi
)
title Anaconda
)
set ANACONDA_SCRIPTS=%ANACONDA%\Scripts
for %%i in ("python.exe") do (
for %%j in ("%ANACONDA%\python.exe") do (
if not "%%~f$PATH:i" == "%%~f$PATH:j" (
set ANACONDA_OLD_PATH="%PATH%"
set PATH=%ANACONDA%;%ANACONDA_SCRIPTS%;%PATH%;
echo Added %ANACONDA% and %ANACONDA_SCRIPTS% to PATH.
)
)
)
if not "%ANACONDA_ENV_NAME%" == "" (
echo Activating environment %ANACONDA_ENV_NAME%...
set PROMPT=[%ANACONDA_ENV_NAME%] $P$G
)
I have very little experience with bat language but I guess there may be something to do with this line
setlocal enableextensions enabledelayedexpansion
I tried to remove that line but kept trapped in the ERRORLEVEL 1 expression with message.
error: unable to enable command extensions
Can anyone suggest what I should do to make this bat-file work properly?
I don't think you need a batch file. Assuming that Anaconda and CMD are on your path (which they should be), you can try this as an alternative (it is what I do):
cmd "/c activate py3k && ipython --pylab"
Recently installed Anaconda (1.9) for my python project on win7
After installation, I built a python 3 support environment with instruction in this page. My next task is to activate my python environment automatically with built-in batch file.
I used the command in [Anaconda Command Prompt] shortcut I found in my start menu. It runs a batch-file called [anaconda.bat]
After observing the batch file I realized it seems to be capable of taking an input argument that is supposed to be the environment I would like to activate. So I copied the shortcut and modified it as
C:\Windows\System32\cmd.exe /k "C:\Anaconda\Scripts\anaconda.bat py3k"
Then I double clicked on the new shortcut, it opened a new command window but...the designated environment did not activate!
#echo off
rem +===========================================================================
rem | Initialisation
rem +===========================================================================
verify bogus-argument 2>nul
setlocal enableextensions enabledelayedexpansion
if ERRORLEVEL 1 (
echo error: unable to enable command extensions
goto :eof
)
for %%i in ("%~dp0..\envs") do (
set ANACONDA_ENVS=%%~fi
)
if not "%1" == "" (
if not exist "%ANACONDA_ENVS%\%1\python.exe" (
echo No environment named "%1" exists in %ANACONDA_ENVS%
goto :eof
)
set ANACONDA_ENV_NAME=%1
set ANACONDA=%ANACONDA_ENVS%\%1
title Anaconda (%ANACONDA_ENV_NAME%^)
) else (
set ANACONDA_ENV_NAME=
for %%i in ("%~dp0..") do (
set ANACONDA=%%~fi
)
title Anaconda
)
set ANACONDA_SCRIPTS=%ANACONDA%\Scripts
for %%i in ("python.exe") do (
for %%j in ("%ANACONDA%\python.exe") do (
if not "%%~f$PATH:i" == "%%~f$PATH:j" (
set ANACONDA_OLD_PATH="%PATH%"
set PATH=%ANACONDA%;%ANACONDA_SCRIPTS%;%PATH%;
echo Added %ANACONDA% and %ANACONDA_SCRIPTS% to PATH.
)
)
)
if not "%ANACONDA_ENV_NAME%" == "" (
echo Activating environment %ANACONDA_ENV_NAME%...
set PROMPT=[%ANACONDA_ENV_NAME%] $P$G
)
I have very little experience with bat language but I guess there may be something to do with this line
setlocal enableextensions enabledelayedexpansion
I tried to remove that line but kept trapped in the ERRORLEVEL 1 expression with message.
error: unable to enable command extensions
Can anyone suggest what I should do to make this bat-file work properly?
I don't think you need a batch file. Assuming that Anaconda and CMD are on your path (which they should be), you can try this as an alternative (it is what I do):
cmd "/c activate py3k && ipython --pylab"
This is my first batch file, and also my first time with batch language (I usually use UNIX and don't know a lot about Windows commands).
I'm creating a batch file called install.bat which does all the work to install a Java application from source files. Here a snapshot of the install section:
#ECHO off
SET INSTALL_DIR=%1\
SET SRC_DIR=sources\
SET LIB_DIR=lib\
SET IMG_DIR=img\
SET BIN_DIR=bin\
SET INIT_DIR=%CD%
SET MAIN_CLASS=%SRC_DIR%\main\Main.java
SET CLASS_PATH=%LIB_DIR%log4j.jar;%LIB_DIR%jdom.jar;
SET JAR_NOM=myApp.jar
SET JAR_MF=MANIFEST.MF
:BEGIN
CLS
ECHO Checking directory...
IF EXIST %INSTALL_DIR% (
GOTO Ask_Overwrite
) ELSE (
GOTO Install
)
:Ask_overwrite
SET OVERW=Y
SET /P OVERW="The program is already installed. Overwrite? ([Y]/N): "
IF %OVERW%==N GOTO Cancel
IF %OVERW%==n GOTO Cancel
IF %OVERW%==Y (
RD /S /Q %INSTALL_DIR% <--- Here was the error
GOTO Install
)
IF %OVERW%==y (
RD /S /Q %INSTALL_DIR% <--- Here was the error
GOTO Install
)
GOTO Ask_overwrite
:Install
MD %INSTALL_DIR%
XCOPY . %INSTALL_DIR% /E
CD /D %INSTALL_DIR%
MD %BIN_DIR%
ECHO Compiling...
javac -cp %CLASS_PATH% -sourcepath %SRC_DIR% %MAIN_CLASS% -d %BIN_DIR%
ECHO Creating JAR file...
jar cfm %JAR_NOM% %JAR_MF% -C %BIN_DIR% .
ECHO Succes! The application has been installed in %INSTALL_DIR%
GOTO CleanUp
:Abort
ECHO Abort! The application has not been installed.
GOTO CleanUp
:Cancel
ECHO Canceled by user. The application has not been installed.
GOTO END
:Cleanup
REM Code for clean up
GOTO END
:END
CD /D %INIT_DIR%
PAUSE
NOTE: The javac and jar commands are correct, at least work in my machine.
Well, the script works correctly when I test it with the INSTALL_DIR belonging to the same drive where I execute it, but if I use a target directory out of the drive where I'm executing, I have problems.
Executions without problems (called in a cmd.exe session):
C:\Users\TC\testInstall> install.bat .\..\installTarget
C:\Users\TC\testInstall> install.bat C:\Users\TC\installTarget
Execution with problems (called in a cmd.exe session):
C:\Users\TC\testInstall> install.bat D:\Documents\installTarget
The problems happen when I try to copy files specially, but also making and removing directories.
I hope someone can tell to me which options I must use in order to fix the problems.
Regards!
Well, I have two mistakes that I fixed and then the script work correctly.
The first was that I didn't use the /d option in cd command in order to change also the drive unit. It means, C:Users\TC> cd D:\Documents is wrong, the correct command is the following: C:Users\TC> cd /d D:\Documents as well as the MS-DOS manual page indicates.
The second error, it wasn't actually an error, I put rm -rf %INSTALL_DIR% (like in Linux) instead of the correct Windows command rd /s/q %INSTALL_DIR%
Now all the problems have been fixed and the script works properly :)
It has to do with batch files not accessing other drives than the C:\ drive by default. Even if you open up your command line, you shouldn't be able to CD D:\. Try this (not sure if this will work as I have never tried it)
PUSHD D:\
C:\Users\TC\testInstall> install.bat D:\Documents\installTarget
Or else, use PUSHD D:\ then move the install file to D:\ temporarily and install. Only solutions I can think of.