unable to move xls file(file name has space in between) from input folder to back up
Hi,
i have a function moveFile to move xls file from input to backup.
My input file name has spaces in between.To handle this i have given quotes inthe beginning and end of source,destination file .
Code :
"mv " +'"'+ strSource + File.separator + strFileName +'"'+ " " + '"' +strDestination + File.separator +'"';
eg: mv "/export/home1/users/test/Input/test space. #
133_May 2009.xls" "/export/home1/users/test/Backup/test space. #
133_May 2009.xls"
when i execute this code my file is not getting moved.How ever if copy & paste the same command in the telnet it works.
if i give a file with no spaces in between it's name
The command "mv " + strSource + File.separator + strFileName + " " +strDestination + File.separator works (no quotes inthe beginning and end of source,destination files)
eg: mv /export/home1/users/test/Input/nospace.xls /export/home1/users/test/Backup/nospace.xls
check my function
*/............../*
Code :
void moveFile( String strFileName, String strSource, String strDestination ) throws Exception{
try{
strCommand = "mv " +'"'+ strSource + File.separator + strFileName +'"'+ " " + '"' +strDestination + File.separator +'"';
Runtime r = Runtime.getRuntime();
Process p = r.exec( strCommand);
}
System.out.println("moved to backup folder "+ strCommand );
}catch( Exception e ){
throw new Exception( "Error While Moving " + strFileName +" from " + strSource + " to " + strDestination + ". ", e);
}
}
Please help me to solve the problem :confused:
Thanks in advance,
Raji
Re: unable to move xls file(file name has space in between) from input folder to back
The reason is passing the string without the quotations (they're removed at compile time). In order to put the quotes inside the string, you must use the \" escape character, so you're passing something like this to the command line:
Code :
mv some file.xls new file.xls
this will try to find the file some and move it to the file file.xls. When you copy it from the code directly to the command line, it recognizes the quotes because they haven't been "hidden away", so the command that's getting passed would be something like this:
Code :
mv "some file.xls" "new file.xls"
Here's what a string with quotes in the string would look like:
Code :
System.out.println("\"This is a string with quotes around it\"");
Re: unable to move xls file(file name has space in between) from input folder to back
Hi helloworld922,
Thanks a lot for your reply .
I tried by changing my code like below
Process p = r.exec( "mv "+"\""+strSource+"/"+strFileName+"\""+" "+"\""+strDestination+"/"+strFileName +"\"");
but my file is not getting moved.please help me to debug the pbm .
Thanks in advance,
Raji
Re: unable to move xls file(file name has space in between) from input folder to back
I haven't looked too far into this, but maybe you could try out the ProcessBuilder class, and have it create a separate console to run the command you want.
Re: unable to move xls file(file name has space in between) from input folder to back
Pass the arguments to exec as an array as opposed to using a single String command, so the spaces don't confuse things. (quoting or escaping shouldn't be necessary, but that behavior may be platform dependent)
Code :
String arguments[] = {"mv", "original file", "moved location" };
Runtime.getRuntime().exec(arguments);
Re: unable to move xls file(file name has space in between) from input folder to back
Hi copeg,
Thanks a lot for your reply .
I tried by changing my code like below
String arguments[] = {"mv", "strSource + File.separator +strFileName"," ", "strDestination + File.separator" };
Runtime r = Runtime.getRuntime();
Process p = r.exec( arguments );
i tried with escape character also but still it's not working :-(
Re: unable to move xls file(file name has space in between) from input folder to back
Quote:
Originally Posted by
raji
I tried by changing my code like below
String arguments[] = {"mv", "strSource + File.separator +strFileName"," ", "strDestination + File.separator" };
Runtime r = Runtime.getRuntime();
Process p = r.exec( arguments );
Check the quotations and verify the correct paths of the src and dest file names. The code below is based upon the variables you referred to in previous posts
Code :
String arguments[] = {"mv", strSource + File.separator + strFileName, strDestination + File.separator +
strFileName};
Re: unable to move xls file(file name has space in between) from input folder to back
Hi copeg,
Thanks a lot for your reply .
I tries as u said.Here the pbm is the space in the file name.When we execute mv command due to the space in between system will consider the data after space as next parameter.Is there any way to move these kind of files.
The below code will work for file with out space in their name
strCommand = "mv "+strSource+"//"+strFileName+" "+strDestination+"//"+strFileName;
Do reply :confused:
Thanks,
Raji
Re: unable to move xls file(file name has space in between) from input folder to back
Quote:
Originally Posted by
raji
I tries as u said.Here the pbm is the space in the file name.When we execute mv command due to the space in between system will consider the data after space as next parameter.Is there any way to move these kind of files.
The standard work around for files with spaces is the solution I outlined above (multiple arguments to exec)...on my system (unix) this works as expected (single argument exec does not). Since exec is a bit of a pain (and its commands system dependent) your mileage may very, but I'm sure if you play around with it using a test file you'll figure it out. Here's another thread over at the sun forums that draws out the same solution I did Runtime.exec is not working when folder is having a white space in its name
.