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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

batch file string parsing

Reply to Message Icon

Name: esteban
Date: June 14, 2008 at 13:21:46 Pacific
OS: Windows Vista Home Premiu
CPU/Ram: Intel Core 2 Duo @ 2.67 G
Product: 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?



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: June 14, 2008 at 17:24:23 Pacific
Reply:

@echo %~dpn1


0

Response Number 2
Name: esteban
Date: June 14, 2008 at 19:22:20 Pacific
Reply:

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?


0

Response Number 3
Name: Razor2.3
Date: June 14, 2008 at 19:26:39 Pacific
Reply:

@echo %~n1


0

Response Number 4
Name: esteban
Date: June 15, 2008 at 06:01:26 Pacific
Reply:

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


0

Response Number 5
Name: Mechanix2Go
Date: June 15, 2008 at 07:13:46 Pacific
Reply:

"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


0

Related Posts

See More



Response Number 6
Name: esteban
Date: June 15, 2008 at 09:38:51 Pacific
Reply:

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


0

Response Number 7
Name: CoderK
Date: July 27, 2008 at 06:47:06 Pacific
Reply:

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


0

Response Number 8
Name: esteban
Date: July 27, 2008 at 09:30:26 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Batch file copy assistanc... How to read the 3rd line ...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: batch file string parsing

Batch file to parse log www.computing.net/answers/programming/batch-file-to-parse-log/16414.html

batch file to parse text file www.computing.net/answers/programming/batch-file-to-parse-text-file/18030.html

Batch File to Parse and Write back www.computing.net/answers/programming/batch-file-to-parse-and-write-back/18101.html