How to run a command in Windows batch script? - windows

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 .......

Related

Loop over files in directory with if statement (batch file)

I'm trying to create a bat file to compile all of the .java files in a directory and to run the class called Main (and output to tulemused.txt) I was able to do this for a single .java file with a set name but I don't know how to use batch scripting well enough to loop over all of the files.
#ECHO on
FOR /d %%f DO IF NOT %%f=="compile.bat" javac -encoding utf-8 %%f
java -Dfile.encoding=UTF8 Main > tulemused.txt
#ECHO off
This is how far I got
Try this:
#ECHO on
FOR /r %%f In (*) DO IF NOT "%%~nf"=="compile.bat" javac -encoding utf-8 "%%~f"
java -Dfile.encoding=UTF8 Main >tulemused.txt
Found that this works even better since java wants to compile all of the classes at once but credit to Wasif for fixing the original loop
#ECHO on
set string=javac -encoding utf-8
FOR /r %%f In (*.java) DO call :concat %%~nf
%string%
set /P name="What is the name of the class with the main method? "
java -Dfile.encoding=UTF8 %name% >result.txt
pause
:concat
set string=%string% %1.java

In windows how to iterate over given directory and do replace operation in file using batch command?

For a given directory, How to find .java file in current and sub-directory and verify if the file containing "com.demo" then replace with "com.practice" in Windows bat command?
Assumptions:
OS: Windows 10
After doing lot of google I came to below command but it neither throwing error nor doing anything.
setlocal enabledelayedexpansion
for %%f in ("<my direcoty path>*.java") do (
type %%f | repl "com.demo" "com.practice" >%%f.new
move %%f.new %%f
set /p val=<%%f
echo %val%
echo "fullname: %%f"
echo "name: %%~nf"
echo "contents: !val!"
)
pause

How to source a Windows batch script

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

Reuse result of "where" command

I need to find the specific location of java 64-bit on a windows machine. I thought about using where java to find all possible locations. In the next step I would need to isolate the proper location which starts with: C:\Program Files\Java\... and then execute the command as following:
cmd /K %var% -jar %~dp0XYZ.jar
Is this the proper way to find the java path which might change over time? If yes, how can I get the path from where into a variable?
For the output of where assume this:
C:\Program Files (x86)\Common Files\Oracle\Java\javapath\java.exe
C:\Program Files\Java\jdk1.8.0_144\bin\java.exe
C:\Program Files\Java\jdk1.8.0_144\jre\bin\java.exe
It wouldn't matter if it takes second or third result, as both are 64 bit in this case. But as I can't guarantee that output, the only way to identify the 64-bit version is with C:\Program Files\Java\
The following bat script (commented for explanation) should do the trick:
#ECHO OFF
rem SETLOCAL EnableExtensions DisableDelayedExpansion
set "_flagexe64=:\Program Files\Java\"
set "_javaexe64="
for /f "delims=" %%G in ('where java.exe') do (
rem next two lines for debugging only
echo checking "%%~G"
"%%~G" -version
if not defined _javaexe64 (
set "_javaexe64=%%~G"
call :check64
)
)
rem final check
if not defined _javaexe64 (
1>&2 echo sorry, no java.exe found under "%_flagexe64%"
goto :eof
)
rem java.exe found:
echo "%_javaexe64%"
goto :eof
:check64
call set "_aux=%%_javaexe64:%_flagexe64%=%%"
if "%_aux%" == "%_javaexe64%" set "_javaexe64="
goto :eof

Java Version in a batch file

My question, is extremely similar to the following SO question: How to get Java Version from batch script?
In fact, it did almost solve my problem. The only difference is that I've to check the Java version based on %JAVA_HOME%, which the user is free to modify. The issue that I'm facing is with this code:
#ECHO OFF
SETLOCAL enableextensions enabledelayedexpansion
IF "%JAVA_HOME%"=="" (
#ECHO Please set JAVA_HOME environment variable
EXIT /B
)
#echo "%JAVA_HOME%"
REM Checking JAVA_VERSION
SET JAVA_VERSION=
FOR /f "tokens=3" %%g in ('"%JAVA_HOME%"\bin\java -version 2^>^&1 ^| findstr /i "version"') do (
SET "JAVA_VERSION=%%g"
)
%JAVA_HOME%% in my system points to "C:\Program Files\jdk1.7.0_25" (notice the space in the path)
Even with the quotes, I get the following error in command line:
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
Any idea as to how to solve this problem? (The comments to the aforementioned article also mentions this issue). I'm working on a Windows 7 machine
FOR /f "tokens=3" %%g in ('"%JAVA_HOME%\bin\java" -version 2^>^&1 ^| findstr /i "version"') do (
SET "JAVA_VERSION=%%g"
)
Edit the %JAVA_HOME% Variable into:
C:\"Program Files"\jdk1.7.0_25
if you want it automated, type
set %JAVA_HOME%=C:\"Program Files"\jdk1.7.0_25
REASON WHY:
The batch file does not accept the quotes; It is identifying them as a single file. So it attempts to find "C:\Program Files\jdk1.7.0_25" NOT as a folder path, but as a folder NAME in your root folder. If you type in
C:\"Program Files"\jdk1.7.0.25 it identifies that "Program Files" is a single file. If there are no redirection operators, It would think that the path would be like this;
C:\Program\Files\jdk1.7.0_25. It worked for me; It should probably work for you.
Hope that helped
-SonorousTwo
I had a similar problem, take a look at my QA: How to get Java version in a batch script subroutine?
From my answer:
It seems that piping the output to findstr was stripping the quotes for some reason.
I managed to fix the problem without Epic_Tonic's workaround (which would be very difficult to do with a path as a parameter).
This should work in your case:
set first=1
for /f "tokens=3" %%g in ('"%JAVA_HOME%\bin\java" -version 2^>^&1') do (
if !first!==1 echo %%~g
set first=0
)
Note: first is for only searching the first line of java -version's output (reference).

Resources