I want to create a folder, create another file of .url type and save the
.url file in the folder and zip the folder. I am sending the code which
I have written and I am getting following error. “No such file or
directory - InstitutionShortcut”
Create a directory
d = Dir.mkdir(‘test’) #creates a directory named ‘test’
Create a file
File.open(‘test/test.txt’, ‘w’) { |f| puts ‘Hello World’ } #creates a new file named test.txt inside the directory ‘test’ and
writes ‘Hello World’ into the file
This is done in the above code by creating the new file inside the
directory itself
Zip the directory
%x{zip -r test.zip test} #This executes the zip command of the underlying operating system
(I’m using Linux) and creates a zip file named test.zip containing the
directory ‘test’ (the -r flag makes the zipping recursive) #If you can’t use that (on Windows), try rubyzip.sourceforge.net/ -
I have not used it but I found it via Google search
thanks Andrew,
The code which you sent is working fine, I am also using a Linux
machine, But the it is not ziping the folder. Is there anything else I
can use.
If you run the zip code in irb, what output do you get?
I get the following:
Irb(main):001:0> %x{zip -r test.zip test}
=> " adding: test/ (stored 0%)\n adding: test/test.txt (stored 0%)\n"
By overwrite the directory, I assume you want to delete the contents of
the directory if it exists, you can do that as follows:
if File.exists?(‘test’) && File.directory?(‘test’)
Dir[‘test/**/*’].reverse.each { |path| if File.file?(path) then
File.delete(path) else Dir.rmdir(path) end }
else
Dir.mkdir(‘test’)
end