Gowtham M
Gowtham M

Reputation: 277

How to run a command in Windows batch script?

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?

Upvotes: 0

Views: 133

Answers (1)

Alex Sveshnikov
Alex Sveshnikov

Reputation: 4329

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

Upvotes: 2

Related Questions