我有一个目录,其中包含带有时间戳的相同脚本的多个日志。我想收集所有的zip文件并制作一个新的zip。
Directory Structure:
Test_1_Run_Logs_06-12-2018_10_15_35.zip
Test_1_Integration_Logs_06-12-2018_10_15_35.zip
Test_1_Interface_Logs_06-12-2018_10_15_35.zip
Test_2_Run_Logs_06-12-2018_10_30_35.zip
Test_2_Integration_Logs_06-12-2018_10_30_35.zip
Test_2_Interface_Logs_06-12-2018_10_30_35.zip
我已将所有具有相同名称的文件分开。 zip文件没有移动所有的zip文件。如何在ruby中完成
Code
require 'fileUtils'
require 'zip'
scriptNameArr = []
logFolder = 'C:/Users/Desktop/logs/'
copyFolder = "C:/Users/admin/Desktop/Test/Ruby Test/copyFolder/"
# Collect all the files present in logFolder separating by timestamp
Dir.entries("#{logFolder}/").each do |fName|
unless (File.directory? "#{logFolder}#{fName}")
scriptNameArr << fName.split("/").last.split(/_\d+-\d+-/)[0]
end
end
scriptNameArr.uniq!
# Create a new zip into copy
scriptNameArr.each do |scriptName|
zipName = "#{copyFolder}#{scriptName}.zip"
Dir.mkdir(copyFolder) unless (Dir.exist?(copyFolder))
FileUtils.rm(zipName) if File.exist? (zipName)
Zip::File.open(zipName, Zip::File::CREATE) do |zip|
Dir.glob("#{logFolder}#{scriptName}*") { |file|
fileName = file.split("/").last
zip.add(fileName, logFolder)
}
end
end
每次创建空拉链。如何复制zip文件并粘贴到新位置?
答案 0 :(得分:0)
我想你zip.add(fileName, logFolder)
时会错过文件名
正确:
zip.add(fileName, File.join(logFolder, fileName))
快乐的编码! :)