This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes spaces in file name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "spaces in file name" Watch "spaces in file name" New topic
Author

spaces in file name

Nawar Gailani
Ranch Hand

Joined: Feb 08, 2005
Posts: 46
Hi there,
I don't know if this is the right place to post, but since I'm using Swing, I thought that many of you have came to this problem.
I have a text field (or label) and there is a button that execute the file name inside this text field or label, the code is:


Now, if archivo = "c:\myFolder\myFile.doc" for example, there is no problem, but if archivo = "c:\Documents and Settings\Nawar\My Documents\SysInfo.txt" for example, I have this error message from Windows:
The system cannot find the file c:\Documents
The problem I see it is in the spaces in the file name. I tried many quotation varieties, but without a success.
I could execute all the files using this:

which works, but always inside Internet Explorer, which is not really what I prefer.
Am I missing something? Or there is a better way to accomplish this?

Thanks in advance for any help.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Did you try:



GenRocket - A Test Data Generation Platform
Nawar Gailani
Ranch Hand

Joined: Feb 08, 2005
Posts: 46
Hi Gregg,
yes I tried it, it just shows the command prompt with the title of archivo value.
I have searched and found
this.
So it seems a problem with cmd in windows as it behaves like the old dos!
Anyway I'm trying now to check the extension of the file, so if it is doc or pdf i open it with word or acrobat repecively, if not i open it with explorer.
If I find a better solution for the spaces in the file name I will post it as a reference.
Thanks for your time
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Well, after reading that link you posted, I tried this...



And it worked great.
Nawar Gailani
Ranch Hand

Joined: Feb 08, 2005
Posts: 46
Originally posted by Gregg Bolinger:
Well, after reading that link you posted, I tried this...



And it worked great.


Yes Gregg, it works great
I prefered that the file be executed like in the first way whether it has spaces or not, because it is just like double clicking a file (in windows explorer for example) and Windows knows which program associated with this extension because I don't know exactly what format this file will be.
Well, after asking my client (my wife actually) what formats mostly she thinks she will use, she said doc 90% and the remain mostly pdf, so I wrote this code:


And the result is ok.

Thanks again for your time and suggestions
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

I'm a bit unclear on why you need to do it that way. I have done the following:



I used actual documents on my PC rather than SysInfo.xxx but everytime I was given the document loaded in the correct application as specified by Windows file type association.
Nawar Gailani
Ranch Hand

Joined: Feb 08, 2005
Posts: 46
Oh! I think I have to drink a strong coffee to wake up!
You already told me the answer! but I thought it is to execute specific program not a file!
My mistake, sorry
The thing is there is another button that browse for a file, then the file path will be stored in the database, when recall the record the path appears on this label and by pressing the first button, the file will open inside the associated program.
Thanks a lot Gregg, I didn't thought to use backslash before the backslash in the string!
Now I only need to check for a backlash in the String and then apend another one before execute it, right?
YES!

[ March 31, 2006: Message edited by: Nawar Gailani ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Now I only need to check for a backlash in the String and then apend another one before execute it, right?

Well, it depends on how you are building that string. If you are getting it from a Java class that is providing the path for you, then you shoulnd't have to. Java should return an appropriately formatted OS specific string.

On the otherhand, if you are building the string manually, then yes, you'll have to do what you posted. Either way, looks like it's working out. So good job.
Nawar Gailani
Ranch Hand

Joined: Feb 08, 2005
Posts: 46
Thanks Gregg
Without your help I would stay with that 'ugly' code and keep complaining how 'cmd' is a stupid command from the old dos days!
I also will not forget your info about getting the String from a Java class.
Cheers!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: spaces in file name
 
Similar Threads
Runtime.getRuntime() and XP directory name spaces
Problem running cmd From Java only.
problems with using Runtime.getRuntime().exec()....
rsync blank spaces problem
Open Help(.chm) File on click on Button