Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a script in Ruby with spaces in filename

Tags:

ruby

I wrote a simple script to check the status of a Dropbox file and move it to another folder, if the file has been synced and downloaded completely (status == 'up to date').

The only problem I have now is, if the file or the directory has spaces in it, then my script won't run correctly.

I used #{...} to pass the filename, but seems that it splits whatever is given to it at whitespaces.

Here is my script:

# runs in Ruby 1.8.x (ftools)                                                                                    

 class DropboxHelper                                                                  

     require 'ftools'                                                                 

     def self.move                                                                    
         directory = "Foo"                                                     
         destination = "Bar"                                                   

         while true                                                                   
             Dir.glob(directory+"/**/*") do |item|                                    
                 next if item == '.' or item == '..'                                  
                 fileStatus = `~/bin/dropbox.py filestatus #{item}`                   
                 puts "processing " + item                                            
                 puts "filestatus: " + fileStatus                                     
                 if (fileStatus.include? "up to date")                                
                     puts item.split('/',2)[1] + " is up to date, starting to move file now."
                     `cp -r #{item + " " + destination + "/" + item.split('/',2)[1]} >> copiedFile.out`

                     # remove file in Dropbox folder, if current item is not a directory and 
                     # copied file is the identical.                                  
                     if (!File.directory?(item) && File.cmp(item, destination + "/" + item.split('/',2)[1]).to_s)
                         puts "remove " + item                                        
                         `rm -rf #{item} >> removedFile.out`                          
                     end                                                              
                 else                                                                 
                     puts item + " is not up to date, moving to next file."           
                 end                                                                  
             end                                                                      

             # execute every hour                                                     
             puts "sleeping now"                                                      
             sleep(3600)                                                              
         end                                                                          
     end                                                                              
 end                                                                                  

 DropboxHelper.move   

When running the script I get following output, if the file or directory has spaces in them. And it even says that a file is not up to date, although it is:

Foo/Name with spaces is not up to date, moving to next file.
processing Foo/Name with spaces/file #1.txt
filestatus: Foo/Name: File doesn't exist
with:                 File doesn't exist
spaces/file:          File doesn't exist
Foo/Name with spaces/file #1.txt is not up to date, moving to next file.

Any suggestions how to improve and fix the code?

like image 634
cherrun Avatar asked Apr 10 '13 14:04

cherrun


2 Answers

Wrap all occurences of #{item} in single-quotes like so '#{item}'. For example:

fileStatus = `~/bin/dropbox.py filestatus '#{item}'`

You need to do this because bash (or whatever shell you're using) interprets spaces in a special way. Since your filename itself contains a space, you need to escape it. Also see 3.3. Quoting characters from the Bash Guide for Beginners for more information.

like image 62
Sahil Muthoo Avatar answered Nov 02 '22 19:11

Sahil Muthoo


You may use the builtin shellescape

fileStatus = `~/bin/dropbox.py filestatus #{item.shellescape}`

http://ruby-doc.org/stdlib-2.3.0/libdoc/shellwords/rdoc/Shellwords.html#method-c-shellescape

like image 41
Nicolas Maloeuvre Avatar answered Nov 02 '22 20:11

Nicolas Maloeuvre