开发者

validate directory path

I'm writing a backup program in Ruby. The user enters the destination directory.

How do i validate the entry? When i intentionally enter something silly like a/:mcjhs, I keep getting an error. (of course lol)

How do i stop this from happening?

Thanks for your help :-)

edit:

this is a sample of what i mean: destination1 cannot be created and throws an error. how do I pick up on that beforehand?

destination1 = 'hj&:test1'
destination = 'f:\test'
if (test(?d,destination1))
    puts "already exists"
    system "pause"
else 
    Dir.mkdir(destination1)
end

I solved it, and yes i did research before too :-P

i put the if statement into a begin/rescue/end statement. so easy. thanks for your help!

begin开发者_StackOverflow中文版
destination1 = 'hj&:test1'
destination = 'f:\test' 
if (test(?d,destination1))
    puts "already exists"
    system "pause" 
else 
    Dir.mkdir(destination1)
end
rescue
    puts "error"
end


you can use if/else?

if File.directory?(destination)
  ....
else
  ....
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜