Need spome help please :)

Hello :slight_smile:

SO here is my problem.
I have this code and need it to work but i cant do it by myself and now
need to ask you if you can help.

  # Changing the file name
    def file_rename(path)
        case path

            when $file_pos_path then $file_name.value = "zdjęcie
stanowiska POS w PSD" and file_ext = $file_pos_ext and old_file =
$file_pos_path
            when $file_kableprzed_path then $file_name.value = "stan
okablowania POS w PSD" and file_ext = $file_kableprzed_ext and old_file
= $file_kableprzed_path
            when $file_kablepo_path then $file_name.value =
"zdjęcie stanowiska POS w PSD" and file_ext = $file_kablepo_ext and
old_file = $file_kablepo_path
            when $file_posprzed_path then $file_name.value = "stan
POS w PSD" and file_ext = $file_posprzed_ext and old_file =
$file_posprzed_path

        end

        if path != nil or "#{path}".length != 0 then
            File.rename("#{old_file}", "Ruch, #{$file_name} #{$psd},
#{$miejscowosc}#{file_ext}")

I would like for this script to only change name of first one like that
but for those last three so it will change it like this:

            File.rename("#{old_file}", "Ruch, #{$file_name} #{$psd}
przed migracją, #{$miejscowosc}#{file_ext}")

Any chance for someone to write it for me in proper way?

Thanks in advance

···

--
Posted via http://www.ruby-forum.com/.

marcin k. wrote in post #1030356:

its a shame that theres noone here who could help

hi Marcin,

  it is not at all clear what the variables ($file_name, $file_pos_path,
old_file, $psd, $miejscowosc, etc.) that you use refer to. the method
`value` which is called is not defined. a method `file_rename` which
takes one argument is defined but never called, and later a `rename`
method which takes 3 arguments is called but never defined. `def`,
`case`, and `if` are all called, yet there is only one `end`. this is
not executable code and that makes it next to impossible to comment
on...

Any chance for someone to write it for me in proper way?

  i doubt that anyone here is willing to write it for you, and i imagine
that's not what you really want either (what fun would that be?!) i'm
sure there are plenty of people reading the forum who would be happy to
help you, but you've got to give folks something to work with :slight_smile:

  - j

···

--
Posted via http://www.ruby-forum.com/\.

well.. it was only pice of code as whole one is long :stuck_out_tongue:
but ok here it is:

require 'tk'

`chcp 65001`
root = TkRoot.new() {title "Migrator RUCH v1.03 sakej update"}

content = Tk::Tile::Frame.new(root) {padding "20 20 125 125"}.grid(
:sticky => 'nsew')
$content = content
TkGrid.columnconfigure root, 0, :weight => 1
TkGrid.rowconfigure root, 0, :weight => 1

# Deklaracja zmiennych globalnych
$psd = TkVariable.new
$miejscowosc = TkVariable.new
$service_check = TkVariable.new

#Ścieżki do plików
$file_protokol_path = TkVariable.new
$file_csq_path = TkVariable.new
$file_serwis_1_path = TkVariable.new
$file_serwis_2_path = TkVariable.new
$file_serwis_3_path = TkVariable.new
$file_modem_path = TkVariable.new
$file_sprzedaz_path = TkVariable.new
$file_plomba_path = TkVariable.new
$file_pos_path = TkVariable.new
$file_kableprzed_path = TkVariable.new
$file_kablepo_path = TkVariable.new
$file_posprzed_path = TkVariable.new
$file_name = TkVariable.new

# Nazwy plików plus rozszerzenia
$file_protokol_name = TkVariable.new
$file_protokol_ext = TkVariable.new
$file_csq_name = TkVariable.new
$file_csq_ext = TkVariable.new
$file_modem_name = TkVariable.new
$file_modem_ext = TkVariable.new
$file_serwis_1_name = TkVariable.new
$file_serwis_1_ext = TkVariable.new
$file_serwis_2_name = TkVariable.new
$file_serwis_2_ext = TkVariable.new
$file_serwis_3_name = TkVariable.new
$file_serwis_3_ext = TkVariable.new
$file_sprzedaz_name = TkVariable.new
$file_sprzedaz_ext = TkVariable.new
$file_plomba_name = TkVariable.new
$file_plomba_ext = TkVariable.new
$file_pos_name = TkVariable.new
$file_pos_ext = TkVariable.new
$file_kableprzed_name = TkVariable.new
$file_kableprzed_ext = TkVariable.new
$file_kablepo_name = TkVariable.new
$file_kablepo_ext = TkVariable.new
$file_posprzed_name = TkVariable.new
$file_posprzed_ext = TkVariable.new
$miejscowosc = TkVariable.new

# Pola na wprowadzenie wartości PSD i Miejscowości
psd = Tk::Tile::Entry.new(content) {width 6; textvariable $psd}.grid(
:column => 2, :row => 1, :sticky => 'we' )
miejscowosc = Tk::Tile::Entry.new(content) {width 20; textvariable
$miejscowosc}.grid(:column => 5, :row => 1, :sticky => 'we')
Tk::Tile::Label.new(content) {text 'PSD'}.grid( :column => 1, :row =>1,
:sticky => 'e')
Tk::Tile::Label.new(content) {text 'Miejscowość'}.grid( :column => 4,
:row => 1, :sticky => 'e')

# Protokół elektroniczny
Tk::Tile::Label.new(content) {text 'Protokol elektroniczny'}.grid(
:column => 1, :columnspan => 2, :row => 2, :sticky => 'e')
Tk::Tile::Button.new(content) {text '...'; command
{Generuj.new.protokol_name(Tk.getOpenFile)}}.grid( :column => 4, :row =>
2, :sticky => 'nw', :padx => 15)
Tk::Tile::Label.new(content) {textvariable $file_protokol_name}.grid(
:column => 5, :row => 2, :sticky => 'ne', :columnspan => 4)

# Zdjęcie modemu
Tk::Tile::Label.new(content) {text 'Zdjęcie modemu'}.grid( :column => 1,
:columnspan => 2, :row => 3, :sticky => 'e')
Tk::Tile::Button.new(content) {text '...'; command
{Generuj.new.modem_name(Tk.getOpenFile)}}.grid( :column => 4, :row => 3,
:sticky => 'nw', :padx => 15)
Tk::Tile::Label.new(content) {textvariable $file_modem_name}.grid(
:column => 5, :row => 3, :sticky => 'ne')

# Zdjęcie CSQ
Tk::Tile::Label.new(content) {text 'Zdjęcie CSQ'}.grid( :column => 1,
:columnspan => 2, :row => 4, :sticky => 'e')
Tk::Tile::Button.new(content) {text '...'; command
{Generuj.new.csq_name(Tk.getOpenFile)}}.grid( :column => 4, :row => 4,
:sticky => 'nw', :padx => 15)
Tk::Tile::Label.new(content) {textvariable $file_csq_name}.grid( :column
=> 5, :row => 4, :sticky => 'ne')

#Zdjęcia 3 pierwszych stron książki serwisowej
Tk::Tile::Label.new(content) {text 'Zdjęcia książki serwisowej'}.grid(
:column => 1, :row => 5, :sticky => 'e')
Tk::Tile::Label.new(content) {text '1'}.grid( :column => 2, :row => 5,
:sticky => 'e')
Tk::Tile::Label.new(content) {text '3'}.grid( :column => 2, :row => 6,
:sticky => 'e')
Tk::Tile::Label.new(content) {text '4'}.grid( :column => 2, :row => 7,
:sticky => 'e')

  Tk::Tile::Button.new(content) {text '...'; command
{Generuj.new.serwis_1_name(Tk.getOpenFile)}}.grid( :column => 4, :row =>
5, :sticky => 'nw', :padx => 15)
  Tk::Tile::Button.new(content) {text '...'; command
{Generuj.new.serwis_2_name(Tk.getOpenFile)}}.grid( :column => 4, :row =>
6, :sticky => 'nw', :padx => 15)
  Tk::Tile::Button.new(content) {text '...'; command
{Generuj.new.seriws_3_name(Tk.getOpenFile)}}.grid( :column => 4, :row =>
7, :sticky => 'nw', :padx => 15)

Tk::Tile::Label.new(content) {textvariable $file_serwis_1_name}.grid(
:column => 5, :row => 5, :sticky => 'ne')
Tk::Tile::Label.new(content) {textvariable $file_serwis_2_name}.grid(
:column => 5, :row => 6, :sticky => 'ne')
Tk::Tile::Label.new(content) {textvariable $file_serwis_3_name}.grid(
:column => 5, :row => 7, :sticky => 'ne')

# Zdjęcie sprzedaż próbnej
Tk::Tile::Label.new(content) {text 'Zdjęcie sprzedaży próbnej'}.grid(
:column => 1, :columnspan => 2, :row => 9, :sticky => 'e')
Tk::Tile::Button.new(content) {text '...'; command
{Generuj.new.sprzedaz_name(Tk.getOpenFile)}}.grid( :column => 4, :row =>
9, :sticky => 'nw', :padx => 15)
Tk::Tile::Label.new(content) {textvariable $file_sprzedaz_name}.grid(
:column => 5, :row => 9, :sticky => 'ne')

# Zdjęcie plomby serwisowej
Tk::Tile::Label.new(content) {text 'Zdjęcie plomby serwisowej'}.grid(
:column => 1, :columnspan => 2, :row => 10, :sticky => 'e')
Tk::Tile::Button.new(content) {text '...'; command
{Generuj.new.plomba_name(Tk.getOpenFile)}}.grid( :column => 4, :row =>
10, :sticky => 'nw', :padx => 15)
Tk::Tile::Label.new(content) {textvariable $file_plomba_name}.grid(
:column => 5, :row => 10, :sticky => 'ne')

# Zdjęcie stanowiska POS
Tk::Tile::Label.new(content) {text 'Zdjęcie stanowiska POS'}.grid(
:column => 1, :columnspan => 2, :row => 11, :sticky => 'e')
Tk::Tile::Button.new(content) {text '...'; command
{Generuj.new.pos_name(Tk.getOpenFile)}}.grid( :column => 4, :row => 11,
:sticky => 'nw', :padx => 15)
Tk::Tile::Label.new(content) {textvariable $file_pos_name}.grid( :column
=> 5, :row => 11, :sticky => 'ne')

# Zdjęcie kabli przed migracją
Tk::Tile::Label.new(content) {text 'Zdjęcie kabli przed migracją'}.grid(
:column => 1, :columnspan => 2, :row => 12, :sticky => 'e')
Tk::Tile::Button.new(content) {text '...'; command
{Generuj.new.kableprzed_name(Tk.getOpenFile)}}.grid( :column => 4, :row
=> 12, :sticky => 'nw', :padx => 15)
Tk::Tile::Label.new(content) {textvariable $file_kableprzed_name}.grid(
:column => 5, :row => 12, :sticky => 'ne')

# Zdjęcie kabli po migracji
Tk::Tile::Label.new(content) {text 'Zdjęcie kabli po migracji'}.grid(
:column => 1, :columnspan => 2, :row => 13, :sticky => 'e')
Tk::Tile::Button.new(content) {text '...'; command
{Generuj.new.kablepo_name(Tk.getOpenFile)}}.grid( :column => 4, :row =>
13, :sticky => 'nw', :padx => 15)
Tk::Tile::Label.new(content) {textvariable $file_kablepo_name}.grid(
:column => 5, :row => 13, :sticky => 'ne')

# Zdjęcie stanowiska POS przed migracją
Tk::Tile::Label.new(content) {text 'Zdjęcie stanowiska POS przed
migracją'}.grid( :column => 1, :columnspan => 2, :row => 14, :sticky =>
'e')
Tk::Tile::Button.new(content) {text '...'; command
{Generuj.new.posprzed_name(Tk.getOpenFile)}}.grid( :column => 4, :row =>
14, :sticky => 'nw', :padx => 15)
Tk::Tile::Label.new(content) {textvariable $file_posprzed_name}.grid(
:column => 5, :row => 14, :sticky => 'ne')

# Generowanie
Tk::Tile::Label.new(content) {text ''}.grid(:column => 2, :columnspan =>
2, :row => 15, :sticky => 'ne')
Tk::Tile::Button.new(content) {text 'Generuj'; command
{Generuj.new.execute}}.grid( :column => 4, :columnspan => 1, :row => 17,
:sticky => 's')

# Zamykanie programu
Tk::Tile::Button.new(content) {text 'Zamknij'; command {
root.destroy}}.grid(:column => 5, :columnspan => 1, :row => 17, :sticky
=> 'w' )

# Czy w psd była książka serwisowa
Tk::Tile::CheckButton.new(content) {text "Brak książki serwisowej";
variable $service_check; onvalue 'brak'; offvalue '1'; command
{Generuj.new.service_check}}.grid( :column => 3, :columnspan => 2, :row
=> 8, :sticky => 'e')

# Wyczyść wszystkie pola
Tk::Tile::Button.new(content) {text 'Wyczyść'; command
{Generuj.new.clear_all}}.grid(:column => 2, :columnspan => 2, :row =>17,
:sticky => 'w')

class Generuj

···

#
  def service_check
    if $service_check == 'brak' then
      $file_serwis_1_name.value = "brak książki serwisowej w PSD"
      $file_serwis_2_name.value = "brak książki serwisowej w PSD"
      $file_serwis_3_name.value = "brak książki serwisowej w PSD"

    else
      $file_serwis_1_name.value = nil
      $file_serwis_2_name.value = nil
      $file_serwis_3_name.value = nil

    end
  end
  # Nazwa pliku protokołu
  def protokol_name(path)
    $file_protokol_path.value = path
    $file_protokol_name.value = File.basename(path, ".*")
    $file_protokol_ext.value = File.extname(path)
  end

  # Nazwa pliku modemu
  def modem_name(path)
    $file_modem_path.value = path
    $file_modem_name.value = File.basename(path, ".*")
    $file_modem_ext.value = File.extname(path)
  end

  # Nazwa pliku csq
  def csq_name(path)
    $file_csq_path.value = path
    $file_csq_name.value = File.basename(path, ".*")
    $file_csq_ext.value = File.extname(path)
  end

  # Nazwa pliku serwis_1
  def serwis_1_name(path)
    $file_serwis_1_path.value = path
    $file_serwis_1_name.value = File.basename(path, ".*")
    $file_serwis_1_ext.value = File.extname(path)
  end

  # Nazwa pliku serwis_2
  def serwis_2_name(path)
    $file_serwis_2_path.value = path
    $file_serwis_2_name.value = File.basename(path, ".*")
    $file_serwis_2_ext.value = File.extname(path)
  end

  # Nazwa pliku serwis_3
  def seriws_3_name(path)
    $file_serwis_3_path.value = path
    $file_serwis_3_name.value = File.basename(path, ".*")
    $file_serwis_3_ext.value = File.extname(path)
  end

  # Nazwa pliku sprzedaży
  def sprzedaz_name(path)
    $file_sprzedaz_path.value = path
    $file_sprzedaz_name.value = File.basename(path, ".*")
    $file_sprzedaz_ext.value = File.extname(path)
  end

  # Nazwa pliku plomby
  def plomba_name(path)
    $file_plomba_path.value = path
    $file_plomba_name.value = File.basename(path, ".*")
    $file_plomba_ext.value = File.extname(path)
  end

  # Nazwa pliku pos
  def pos_name(path)
    $file_pos_path.value = path
    $file_pos_name.value = File.basename(path, ".*")
    $file_pos_ext.value = File.extname(path)
  end

  # Nazwa pliku kable przed migracją
  def kableprzed_name(path)
    $file_kableprzed_path.value = path
    $file_kableprzed_name.value = File.basename(path, ".*")
    $file_kableprzed_ext.value = File.extname(path)
  end

  # Nazwa pliku kable po migracji
  def kablepo_name(path)
    $file_kablepo_path.value = path
    $file_kablepo_name.value = File.basename(path, ".*")
    $file_kablepo_ext.value = File.extname(path)
  end

  # Nazwa pliku pos przed migracją
  def posprzed_name(path)
    $file_posprzed_path.value = path
    $file_posprzed_name.value = File.basename(path, ".*")
    $file_posprzed_ext.value = File.extname(path)
  end

  # Zmień nazwę pliku
  def file_rename(path)
    case path
      when $file_protokol_path then $file_name.value =
String.new("potwierdzenie migracji w PSD") and file_ext =
$file_protokol_ext and old_file = $file_protokol_path
      when $file_modem_path then $file_name.value = "potwierdzenie
inslatacji modemu GSM w PSD" and file_ext = $file_modem_ext and old_file
= $file_modem_path
      when $file_csq_path then $file_name.value = "zasięg
stanowiska GSM w PSD" and file_ext = $file_csq_ext and old_file =
$file_csq_path
      when $file_serwis_1_path then $file_name.value = "zdjęcie
książki serwisowej w PSD (1)" and file_ext = $file_serwis_1_ext and
old_file = $file_serwis_1_path
      when $file_serwis_2_path then $file_name.value = "zdjęcie
książki serwisowej w PSD (2)" and file_ext = $file_serwis_2_ext and
old_file = $file_serwis_2_path
      when $file_serwis_3_path then $file_name.value = "zdjęcie
książki serwisowej w PSD (3)" and file_ext = $file_serwis_3_ext and
old_file = $file_serwis_3_path
      when $file_sprzedaz_path then $file_name.value = "potwierdzenie
sprzedaży próbnej w PSD" and file_ext = $file_sprzedaz_ext and old_file
= $file_sprzedaz_path
      when $file_plomba_path then $file_name.value = "zdjęcie
plomby drukarki w PSD" and file_ext = $file_plomba_ext and old_file =
$file_plomba_path
      when $file_pos_path then $file_name.value = "zdjęcie
stanowiska POS w PSD" and file_ext = $file_pos_ext and old_file =
$file_pos_path
      when $file_kableprzed_path then $file_name.value = "stan
okablowania POS przed migracją w PSD " and file_ext =
$file_kableprzed_ext and old_file = $file_kableprzed_path
      when $file_kablepo_path then $file_name.value = "stan
okablowania POS po migracji w PSD " and file_ext = $file_kablepo_ext and
old_file = $file_kablepo_path
      when $file_posprzed_path then $file_name.value = "stan POS po
migracji w PSD " and file_ext = $file_posprzed_ext and old_file =
$file_posprzed_path

    end
    if path != nil or "#{path}".length != 0 then
      File.rename("#{old_file}", "Ruch, #{$file_name} #{$psd} ,
#{$miejscowosc}#{file_ext}")
    #else
    # name_of_file = "Brak pliku Ruch, #{file_name}.txt"
    # file = File.new(name_of_file, "w")
    # file.write "Brak danych dla pliku: Ruch, #{file_name}.txt"
    # file.close
    end

    $file_name.value = nil

  end

  # Zmień nazwy plików
  def execute
    if "#{$psd}".length != 6 or "#{$psd}".to_i == 0 then
      Tk::messageBox :message => "Zły kod psd"
    else
      if not File.directory?("PSD #{$psd}, #{$miejscowosc}") then
        Dir.mkdir( "PSD #{$psd}, #{$miejscowosc}" )
      end
      Dir.chdir( "PSD #{$psd}, #{$miejscowosc}" )

      #$win = TkToplevel.new($content)
      #$win['geometry'] = '300x200+500+200'
      #Tk::Tile::Button.new($win) {text 'Zamknij'; command
{$win.destroy}}.grid( :column => 3, :row => 3, :sticky => 'w')

            #Tk::Tile::Label.new($win) {textvariable $file_name}.grid(
:column => 2, :row => 2, :sticky => 'we');

      # Zmień nazwę protokołu
      file_rename($file_protokol_path)
      file_rename($file_modem_path)
      file_rename($file_csq_path)
      file_rename($file_serwis_1_path)
      file_rename($file_serwis_2_path)
      file_rename($file_serwis_3_path)
      file_rename($file_sprzedaz_path)
      file_rename($file_plomba_path)
      file_rename($file_pos_path)
      file_rename($file_kableprzed_path)
      file_rename($file_kablepo_path)
      file_rename($file_posprzed_path)
      if $service_check == "brak" then
        file = File.new( "Brak książki serwisowej w PSD #{$psd},
#{$miesjcowosc}")
      end

      if all_files_correct? then
        Tk::messageBox :message => "Wykonano pomyślnie"
      else
        Tk::messageBox :message => "Brakuje plików"
      end
    end
    Dir.chdir("..")

  end

  # Uzupełnij braki
  def complete_rest
    if File.exist?("{$protokol_new}")
      puts "isntnieje!"
    else
      puts "nie ma mnie!"
    end
  end

  # Wyczyść wszystkie pola
  def clear_all
    $miejscowosc.value = nil
    $psd.value = nil
    $file_protokol_path.value = nil
    $file_csq_path.value = nil
    $file_serwis_1_path.value = nil
    $file_serwis_2_path.value = nil
    $file_serwis_3_path.value = nil
    $file_modem_path.value = nil
    $file_sprzedaz_path.value = nil
    $file_plomba_path.value = nil
    $file_pos_path.value = nil
    $file_kableprzed_path.value = nil
    $file_kablepo_path.value = nil
    $file_posprzed_path.value = nil
    $file_protokol_name.value = nil
    $file_protokol_ext.value = nil
    $file_csq_name.value = nil
    $file_csq_ext.value = nil
    $file_modem_name.value = nil
    $file_modem_ext.value = nil
    $file_serwis_1_name.value = nil
    $file_serwis_1_ext.value = nil
    $file_serwis_2_name.value = nil
    $file_serwis_2_ext.value = nil
    $file_serwis_3_name.value = nil
    $file_serwis_3_ext.value = nil
    $file_sprzedaz_name.value = nil
    $file_sprzedaz_ext.value = nil
    $file_plomba_name.value = nil
    $file_plomba_ext.value = nil
    $file_pos_name.value = nil
    $file_pos_ext.value = nil
    $file_kableprzed_ext.value = nil
    $file_kableprzed_name.value = nil
    $file_kablepo_ext.value = nil
    $file_kablepo_name.value = nil
    $file_posprzed_ext.value = nil
    $file_posprzed_name.value = nil
    $miejscowosc.value = nil

  end

  # Wszystkich plików
  def all_files_correct?
    if $service_check == "brak" then
      @all_files = 6
    else
      @all_files = 9
    end
      i = 0
      Dir['*.*'].each do |k|
        i = i + 1
      end
      if i == @all_files then
        return true
      else
        return false
      end
    end

  end

#end
Tk.mainloop

--
Posted via http://www.ruby-forum.com/.

well its only pice of the code .. everything is called before or after.

The problem is

i have 12 photos and i want to be able to select them in browse window
and than i want program to rename it according to specific rules where
we have 2 changables and are difined by user everytime when the
application starts

where 1st one is PSD number and second it cityname

so first 9 photos should looks like this

xxx,xxxxxxxxxxxxxxx xxxxxxxxxxxx PSD <first changable>, <second

and last 3 photosh should looks like this

xxx, xxxxxxxxxxxxxx xxxxxxxxxxx PSD <first changable> po migracji ,
<second changable>

···

--
Posted via http://www.ruby-forum.com/\.

hi Marcin,

  i often find that trying to work from within something so large gets
confusing and distracting - a good idea might be to begin by reducing
what you want to do down to the bare minimum. try to write a simple
method that renames files based on parameters that you pass to the
method, and then incorporate that into your larger code.

  - j

···

--
Posted via http://www.ruby-forum.com/.

I am not sure I understand what you are trying to achieve. I do
notice though a highly repetitive structure of your code which means
you should be able to refactor a lot of this into much smaller code
base with clearer structure. For example, you have sets of global
variables (generally a bad idea btw.) which follow common naming
conventions and are treated similarly. In that case it's better to
have collections (either Hash or Array) store the stuff and manipulate
it there. The code might also benefit from some class definitions of
your own.

Also not everybody here is able to read Polish. So it would be easier
for us to help if you change the commenting and variable and method
naming to English.

Kind regards

robert

···

On Sun, Nov 6, 2011 at 9:09 AM, marcin k. <doreklamster@gmail.com> wrote:

well its only pice of the code .. everything is called before or after.

The problem is

i have 12 photos and i want to be able to select them in browse window
and than i want program to rename it according to specific rules where
we have 2 changables and are difined by user everytime when the
application starts

where 1st one is PSD number and second it cityname

so first 9 photos should looks like this

xxx,xxxxxxxxxxxxxxx xxxxxxxxxxxx PSD <first changable>, <second
>

and last 3 photosh should looks like this

xxx, xxxxxxxxxxxxxx xxxxxxxxxxx PSD <first changable> po migracji ,
<second changable>

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/