Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: unable to move xls file(file name has space in between) from input folder to back up

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 .
    "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
    */............../*
    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

    Thanks in advance,
    Raji
    Last edited by helloworld922; December 31st, 2009 at 12:05 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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:

    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:

    mv "some file.xls" "new file.xls"

    Here's what a string with quotes in the string would look like:
    System.out.println("\"This is a string with quotes around it\"");
    Last edited by helloworld922; December 31st, 2009 at 12:09 AM.

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

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

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,343
    Thanks
    182
    Thanked 831 Times in 774 Posts
    Blog Entries
    5

    Default 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)

    String arguments[] = {"mv", "original file", "moved location" };
     
    Runtime.getRuntime().exec(arguments);

  6. #6
    Junior Member
    Join Date
    Dec 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 :-(

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,343
    Thanks
    182
    Thanked 831 Times in 774 Posts
    Blog Entries
    5

    Default Re: unable to move xls file(file name has space in between) from input folder to back

    Quote Originally Posted by raji View Post
    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

    String arguments[] = {"mv", strSource + File.separator + strFileName, strDestination + File.separator + 
     strFileName};

  8. #8
    Junior Member
    Join Date
    Dec 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


    Thanks,
    Raji

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,343
    Thanks
    182
    Thanked 831 Times in 774 Posts
    Blog Entries
    5

    Default Re: unable to move xls file(file name has space in between) from input folder to back

    Quote Originally Posted by raji View Post
    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
    .

Similar Threads

  1. Replies: 1
    Last Post: April 7th, 2011, 07:01 AM
  2. Unable to create a new connection!
    By fh84 in forum JDBC & Databases
    Replies: 1
    Last Post: November 20th, 2009, 04:58 PM
  3. Input file parsing to insert into DB
    By IDForums in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 30th, 2009, 02:29 AM
  4. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM
  5. Java program to take new files from one path and sent it do another path?
    By jazz2k8 in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: July 8th, 2008, 06:47 AM