Topic: MassStorage DriverPack to Sysprep Parser
Some programming and after few hours I have created this Ruby script to parse DriverPack_MassStorage_wnt5_x86-32.ini to sysprep.inf file lines. It accepts disableIfOS= and skipIfOS= switches...now it checks file tags and offers inf files in current folder if check bad...sleepless night, hours and hours of hard work...enjoy!
UPDATED 08-05-22
* folder blocks without dis_os, skip_os were skipped
UPDATED 08-05-23
* major changes
+ checking of folders and tags with tags control (IMPORTANT!)
class SysprepHwidsParser
  #Get formated string input from user 
  def get_string_input
    print '> '
    operation = gets.strip
    puts
    return operation.to_s
  end
  
  # Parse driver files from folders to arrays and return to compare with tags
  def get_files(files_source_path)
  
    # Set variables
    files_array = Array.new
    folders_hash = Hash.new
    
    # Load folders tree to array
    folders_tree = Dir[files_source_path + '**/']
    
    # Cut root and proceed every dir
    folders_tree.slice!(0)
    # Generate hash folders - file arrays
    folders_tree.each do |dir|
      files_array = (Dir.entries(dir))
      back_slash_dir = dir.gsub("/", "\\")
      folders_hash[back_slash_dir] = files_array
    end
    #gets
    # Returns hash folders - file arrays
    return folders_hash
  end
  
# Gets source file from user input
def load_file(source_string)
  puts "Type file name to load data from...[without .ini please]"
  puts "Default file to load is: #{source_string}"
  puts
  puts "For default loading file please press enter..."
  input_file = gets.strip.to_s
  
  # Default file load
  if input_file == ""
    input_file = source_string
  end
  
  # Parse path and return
  file_path = input_file + ".ini"
  return file_path
end
def parse_line(line, output_string_path)
  #hwids_array = Array.new
  
  # Search for device name and parse
  if line.include?("deviceName=")
    device_string = line.split('"')
    sysprep_device = device_string[1]
    return sysprep_device
  
  # Search for folder def. and parse (UPD. 08-05-22)
  else if (line.include?("[") and not line.count(';') >= 1)
      del_left = line.delete("[")
      del_both = del_left.delete("]")
      path_format = del_both.sub("-", "\\")
      sysprep_folder = output_string_path + path_format
      sysprep_folder = sysprep_folder.chop
      return sysprep_folder
    
    # Search for hwids and parse
    else if line.include?("hwids=")
        hwids_string = line.split('"')
        sysprep_hwids = hwids_string[1].split(',')
        return sysprep_hwids
      
      # Search for file tag and parse
      else if line.include?("tag=")
          tag_string = line.split('"')
          tag_file = tag_string[1] + ".inf"
          sysprep_tag = "\\" + tag_file
          return sysprep_tag
        
        # Search for skip and disable strings and parse          
        else if line.include?('disableIfOS=') or line.include?('skipIfOS=')
            os_string = line.split('"')
            sysprep_os = os_string[1].split(',')
            return [sysprep_os]
          end
        end
      end
    end
  end
end
# Parse input data and writes them to specified file
def parse_file(filename, output_string_path, output_file, output_type, files_source_path)
  # Set prelimitaries
  input_array = Array.new
  block_array = Array.new
  output_array = Array.new
  
  # Start time stamp
  time_stamp_start = Time.now
  
  # Load text file data to array
  File.open(filename) do |file_line|
    file_line.each do |current_line|
      input_array << current_line
    end
  end
  #gets
  # Load directory data to array
  dir_structure = get_files(files_source_path)
  
  # Set prelimitaries
  l = input_array.length - 1
  check_strt = nil
  check_stop = nil
  foldr_temp = nil
  dstnc_step = 0
  dev_nmbr = 0
  dis_stop = false
  skp_stop = false
  folders_count = -2
  tag_check = false
  
  # Check empty field
  if input_array.empty? != true
    # Go through every item in array
    0.upto(l) do |x|
      
      # Check folder def., set start, set device number, set temp folder for multiple devices in one folder use
      if (input_array[x].include?("[") and not input_array[x].count(';') >= 1)
        foldr_temp = parse_line(input_array[x], output_string_path)
        check_strt = 'strt'
        folders_count += 1
        if output_array.empty? == false and dev_nmbr != 0 and dstnc_step == 4
          dev_nmbr = dev_nmbr
        else dev_nmbr = 0
        end
      end
      # Check device, set count device, set start for multiple devices, add temp folder to array
      if input_array[x].include?('deviceName=')
        dev_nmbr += 1
        check_strt = 'strt'
        block_array << foldr_temp
      end
      #gets
      # Check hwids, set distance step for skip and disable os checks, set stop
      if input_array[x].include?('hwids=')
        dstnc_step = 1
        if (parse_line(input_array[x], output_string_path)) != nil
          block_array << parse_line(input_array[x], output_string_path)
        end
          check_stop = 'stop'
      end
      # Parse line if block complete and ad to array
      if check_strt == 'strt' and check_stop != 'stop' and dev_nmbr >= 1 and not (input_array[x].include?("[") and not input_array[x].count(';') >= 1) and (parse_line(input_array[x], output_string_path)) != nil
        
        # Made tag to file check and parse line
        temp_line = (parse_line(input_array[x], output_string_path))
        if temp_line != nil and input_array[x].include?('tag=')
          file_array = dir_structure[foldr_temp + "\\"]
          #gets
          0.upto(file_array.length - 1) do |fl_to_tg|
            #gets
            if ((file_array[fl_to_tg]).downcase).include?((temp_line.split('\\')[1].downcase)) == true
              tag_check = true
            else
            end
          end
          
          if tag_check != true  
            puts "Tag error found! Details:"           
            puts foldr_temp + "\\"
            puts temp_line.split('\\')[1].downcase
            puts
            0.upto(file_array.length - 1) do |f|
              if file_array[f].include?('.inf')
                puts "Replace with file [y/n]?"
                puts file_array[f].downcase
                
                #Get user input with formating or recall
                user_input = get_string_input.upcase
                while ((user_input =~ /^[YN]$/).nil?) and (user_input.length != 1) do 
                  puts ">Error. Type [y/n]!"
                  puts
                  user_input = get_string_input.upcase
                end
                
                if user_input.upcase == "Y"
                  temp_line = "\\" + file_array[f].downcase
                else
                end
              else
              end
            end
            puts "--------------------------------"
          else
          end
        end
        block_array << temp_line
        tag_check = false
        temp_line = nil
        file_array = nil
        end
      # Check disable os and set flags (default wxp - may be changed if necessary)
      if input_array[x].include?('disableIfOS=')
        os_split_01 = input_array[x].split('"')
        os_split_02 = os_split_01[1].split(',')
        #gets
        if (os_split_02[0] == "wxp" or os_split_02[1] == "wxp" or os_split_02[2] == "wxp")
          dis_stop = 'stop'
        else
          dis_stop = 'ok'
        end
      end
      # Check skip os and set flags (default wxp - may be changed if necessary)
      if input_array[x].include?('skipIfOS=')
        os_split_01 = input_array[x].split('"')
        os_split_02 = os_split_01[1].split(',')
        #gets
        if (os_split_02[0] == "wxp" or os_split_02[1] == "wxp" or os_split_02[2] == "wxp")
          skp_stop = 'stop'
        else
          skp_stop = 'ok'
        end
      end
      #gets
      # Check if block complete and add temp array items to different array, set flags nil, clear temp array
      if check_stop == 'stop'
        output_array << block_array[1]
        output_array << block_array[3]
        output_array << block_array[0]
        output_array << block_array[2]
        block_array.clear
        check_strt = nil
        check_stop = nil
        #gets
      end
      #gets
      # Check if block complete, check devices, check stop flags, write output, set flags nil, set distance, clear temp array
      if (output_array.empty? != true) and (dstnc_step == 4) and (dev_nmbr >= 1) and not ((dis_stop == 'stop') or (skp_stop == 'stop'))
        write_output(output_file, output_array, output_type)
        dis_stop = nil
        skp_stop = nil
        dstnc_step = 0
        output_array.clear
      end
      # Clear temp array if one of os flag were - stop
      if output_array.empty? == false and (dstnc_step > 4)
        dis_stop = nil
        skp_stop = nil
        output_array.clear
        dstnc_step = 0
      end
      # Set distance count
      if dstnc_step != 0
        dstnc_step += 1
      end
      
      # Next line
      x += 1
    end
  
    # Catch empty array
    else puts "Empty input array. Check if input file not empty!"
  end
  
  # Set time stamp off
  time_stamp_stop = Time.now
  
  # Count time stamp
  run_time = time_stamp_stop - time_stamp_start
  
  # Write time stamp
  # write_timestamp(output_file, run_time) (UPD. 08-05-22)
  write_timestamp(output_file, run_time)
end
# Write time stamp
def write_timestamp(output_file, run_time)
  File.open(output_file , "a+") do |file|
    #file.puts
    #file.puts ";Created on: #{Time.now} / Runtime: #{run_time} sec"
    #file.puts ";Script:#{self} / By: Vaclav VESELY (www.deltaori.com)"
    puts
    puts "Created on: #{Time.now} / Runtime: #{run_time} sec"
    puts "Script:#{self} / By: Vaclav VESELY (www.deltaori.com)"
  end
end
# Write array to defined file
def write_output(output_file, output_array, output_type)
  
  # Set temp array prelimitaries
  write_array = Array.new
  
  # Opens file for append
  File.open(output_file , "a+") do |file|
    #gets
    
    # EXAMPLE: (UPD. 08-05-22)
    # #http://technet2.microsoft.com/windowsserver/en/library/f1ec661a-cdd2-4d73-8471-7ca07f23c5b81033.mspx?mfr=true
    #
    # [SysprepMassStorage]
    # PCI\VEN_1077&DEV_1080 = "C:\Sysprep\qlogic\qlogic.inf", "\nt", "Qlogic Software Disk", "\qlogic"
    
    # Make line for every hwid and conc. with path and file name (UPD. 08-05-22)
      0.upto(output_array[1].length - 1) do |i|
        write_array << [output_array[1][i], output_array[2], output_array[3]]
      end
    
    if output_type == 'txt'
      # Puts device header
      file.puts ';' + output_array[0]
 
      # Write line for every hwid
      0.upto(write_array.length - 1) do |x|
        file.puts write_array[x][0] + ' = ' + '"' + write_array[x][1] + write_array[x][2] + '"'
      end
    else if output_type == 'csv'
        # Write line for every hwid
        0.upto(write_array.length - 1) do |x|
          file.puts write_array[x][0] + ',' + ' = ' + ',' + '"' + write_array[x][1] + write_array[x][2] + '"' + ',' + ';' + output_array[0]
        end
      end
    end
  end
end
# Class end
end
# Set constants
files_source_path = 'C:/SYSPREP/DRIVERS/M/'
output_string_path = 'C:\\SYSPREP\\DRIVERS\\M\\'
source_string = "DriverPack_MassStorage_wnt5_x86-32"
output_txt = "hwids.txt"
#output_csv = "hwids.csv"
# Run functions
finalize = SysprepHwidsParser.new
source_file = finalize.load_file(source_string)
finalize.parse_file(source_file, output_string_path, output_txt, 'txt', files_source_path)
#finalize.parse_file(source_file, output_string_path, output_csv, 'csv', files_source_path)Last edited by deltaorionis (2008-05-24 03:58:40)
