Computing.Net > Forums > Programming > batch file string parsing

batch file string parsing

Reply to Message Icon

Original Message
Name: esteban
Date: June 14, 2008 at 13:21:46 Pacific
Subject: batch file string parsing
OS: Windows Vista Home Premiu
CPU/Ram: Intel Core 2 Duo @ 2.67 G
Model/Manufacturer: Dell XPS 410
Comment:

I'm having a little trouble with parsing strings in batch. Basically, I'm trying to make a batch script to automatically compile and run Java files, and make that script the default program for .java files. The compiling part works fine, but the running part is harder. The JRE takes as an argument just the filename, without an extension (eg for a file named Test.java with bytecode Test.class, one would type "java Test"). My batch code so far looks like this:

"C:\Program Files\Java\jdk1.5.0_12\bin\javac.exe" %1
set raw=%1
set no_period=%raw:.= %
set no_quotes=%no_period:"=%
call cr_parse %no_quotes%
"C:\Program Files\Java\jre1.6.0_05\bin\java.exe" %first_word%

This takes the argument, which is the full file path in quotes, and compiles it. Then it strips off the quotes and replaces the period in the file name with a space. The called file cr_parse simply the first argument passed to it in the variable %first_word%, in this case the file path and file name, but not the extension. For example, this input:

"C:\Documents\Test.java"

would give this to the JRE:

C:\Documents\Test

with no quotes. The problem is, I need to just give it the file name, in this case "Test". How could I do this?


Report Offensive Message For Removal

Response Number 1
Name: Razor2.3
Date: June 14, 2008 at 17:24:23 Pacific
Subject: batch file string parsing
Reply: (edit)

@echo %~dpn1


Report Offensive Follow Up For Removal

Response Number 2
Name: esteban
Date: June 14, 2008 at 19:22:20 Pacific
Subject: batch file string parsing
Reply: (edit)

For me, this is just giving me the full path without the extension. I can already get this; is there a way to get just the filename, without the path?


Report Offensive Follow Up For Removal

Response Number 3
Name: Razor2.3
Date: June 14, 2008 at 19:26:39 Pacific
Subject: batch file string parsing
Reply: (edit)

@echo %~n1


Report Offensive Follow Up For Removal

Response Number 4
Name: esteban
Date: June 15, 2008 at 06:01:26 Pacific
Subject: batch file string parsing
Reply: (edit)

That one does basically the same thing, but without the drive letter. For example, it might give:

\Documents\Test

How can I get just the filename, in this case:

Test


Report Offensive Follow Up For Removal

Response Number 5
Name: Mechanix2Go
Date: June 15, 2008 at 07:13:46 Pacific
Subject: batch file string parsing
Reply: (edit)

"That one does basically the same thing"

Not obvious how. Better check your typing.


=====================================
If at first you don't succeed, you're about average.

M2


Report Offensive Follow Up For Removal


Response Number 6
Name: esteban
Date: June 15, 2008 at 09:38:51 Pacific
Subject: batch file string parsing
Reply: (edit)

Ah, you're right; I was using %~pn1. My bad. Thanks for the help!


Report Offensive Follow Up For Removal

Response Number 7
Name: CoderK
Date: July 27, 2008 at 06:47:06 Pacific
Subject: batch file string parsing
Reply: (edit)

If you just want to compile and run the java program from a single batch file, why don't you try this one?

Hope you have already set the java bin path and class path in My Computer>Properties>Advanced>Environment Variables.
If not then you can add it in the batch file. of course it is as simple as...
set path = %path%;<path of java bin directory>
set classpath = %classpath%;.;<path of jre lib directory>

rem lets call this batch file as execute.bat
javac %1.java
java %1

Obviously you have to supply only the name (without the .java extension) as the argument of the batch file.
e.g.
execute test

Would you like to make this batch file more compact i.e. containing less than 2 lines. ;)


Report Offensive Follow Up For Removal

Response Number 8
Name: esteban
Date: July 27, 2008 at 09:30:26 Pacific
Subject: batch file string parsing
Reply: (edit)

That wouldn't really work for my purpose; what I need is a batch which I can set as the default program to open a .java file with - ie, if you double click on a .java file, that's what is run. Therefore, what will be passed to it is the full name and path of the file. This is the code which I have been using, and it works well:

@echo off
echo Compiling...
echo.
"C:\Program Files\Java\jdk1.6.0_06\bin\javac.exe" %1
echo.
echo Done!
echo Running...
echo.
echo Program output:
echo.
"C:\Program Files\Java\jre1.6.0_06\bin\java.exe" %~n1
echo.
echo Execution complete.
pause


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: batch file string parsing

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software