我有一个包含以下zip文件的文件夹:
13162.zip 14864.zip 19573.zip 20198.zip
在控制台中,当我跑:
cd my_folder; echo `ls *{.zip,.ZIP}`
我得到了以下输出(这是完美的):
ls: cannot access *.ZIP: No such file or directory
13162.zip 14864.zip 19573.zip 20198.zip
现在,当我在ruby中尝试相同时:
cmd= "cd my_folder; echo `ls {*.zip,*.ZIP}`";
puts `#{cmd}`
它只显示:
ls: cannot access {*.zip,*.ZIP}: No such file or directory
=> nil
我尝试这个解决方案: Getting output of system() calls in Ruby
但在我看来这似乎不起作用。
如何在ruby和shell中获得相同的输出?
答案 0 :(得分:2)
您可以将Dir.glob
与File::FNM_CASEFOLD
一起用于不区分大小写的搜索:
Dir.chdir 'my_folder' do
Dir.glob('*.zip', File::FNM_CASEFOLD).each do |zip_file|
puts zip_file
end
end
#=>
# 19573.zip
# 13162.zip
# 14864.zip
# 20198.zip
# 12345.zIp
您可以使用find
进行不区分大小写的搜索:
paths = `find my_folder -maxdepth 1 -iname '*.zip'`.split
#=> ["my_folder/19573.zip", "my_folder/13162.zip", "my_folder/14864.zip", "my_folder/20198.zip", "my_folder/12345.zIp"]
-printf '%P'
也可用于仅显示文件名:
files = `find my_folder -maxdepth 1 -iname '*.zip' -printf '%P\n'`.split
#=> ["19573.zip", "13162.zip", "14864.zip", "20198.zip", "12345.zIp"]
答案 1 :(得分:0)
我认为这应该直接在终端上运行:
system("cd my_folder; ls {*.zip,*.ZIP}")
或创建包含以下内容的ruby文件
NSTableViewDelegate
然后执行它。编写 ls 后,您不需要 echo !