home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 April / Chip_2003-04_cd1.bin / zkuste / phpgtk / download / samples / porg.php < prev    next >
PHP Script  |  2002-08-25  |  3KB  |  100 lines

  1. <?php
  2.     /*
  3.     * porg.php - PORG Organizes Real Good
  4.     *
  5.     * Author: Josh Sherman
  6.     * Purpose: Renames a directory of files based
  7.     *on a custom prefix. i.e. PORGn.*
  8.     * Usage: php -q porg.php
  9.     */
  10.     if (!class_exists('gtk')) {
  11.         if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
  12.             dl('php_gtk.dll');
  13.         else
  14.             dl('php_gtk.so');
  15.     }
  16.     function delete_event()
  17.     {
  18.         return false;
  19.     }
  20.     function destroy()
  21.     {
  22.         Gtk::main_quit();
  23.     }
  24.     function back_up()
  25.     {
  26.         global $dir_entry;
  27.         global $directory;
  28.         $directory = $dir_entry->get_text();
  29.         @mkdir("$directory/bkup", 0777);
  30.         if ($dir = @opendir("$directory")) {
  31.             while (($file = readdir($dir)) !== false) {
  32.                 if ($file != "bkup" && substr($file, 0, 1) != "." && is_dir($file) == 0) {
  33.                     if (@copy("$directory/$file", "$directory/bkup/$file")) {
  34.                         unlink("$directory/$file");
  35.                     }
  36.                 }
  37.             }
  38.             closedir($dir);
  39.         }
  40.         rename_files();
  41.     }
  42.     function rename_files()
  43.     {
  44.         global $directory;
  45.         global $prefix;
  46.         global $prefix_entry;
  47.         global $check;
  48.         global $window;
  49.         $prefix = $prefix_entry->get_text();
  50.         $i = 0;
  51.         if ($dir = opendir("$directory/bkup")) {
  52.             while (($file = readdir($dir)) !== false) {
  53.                 if (strlen($i) == 1) { $number = "000" . $i; }
  54.                 if (strlen($i) == 2) { $number = "00" . $i; }
  55.                 if (strlen($i) == 3) { $number = "0" . $i; }
  56.                 $extension = substr(strrchr($file, "."), 1);
  57.                 if ($file != "." && $file != ".." && $file != "bkup") {
  58.                     if (@copy("$directory/bkup/$file", "$directory/$prefix$number.$extension")) {
  59.                         if ($check->get_active() == 0) {
  60.                             unlink("$directory/bkup/$file");
  61.                         }
  62.                         $i++;
  63.                     }
  64.                 }
  65.             }
  66.             closedir($dir);
  67.         }
  68.         if ($check->get_active() == 0) {
  69.             rmdir("$directory/bkup");
  70.         }
  71.  
  72.         echo "\nall done!\n";
  73.  
  74.     }
  75.     $window = &new GtkWindow();
  76.     $window->set_title('PORG');
  77.     $window->connect('destroy', 'destroy');
  78.     $window->connect('delete-event', 'delete_event');
  79.     $window->set_border_width(10);
  80.     $table = &new GtkTable(4, 2);
  81.     $table->set_row_spacings(4);
  82.     $table->set_col_spacings(4);
  83.     $window->add($table);
  84.     $dir_label = &new GtkLabel('Directory: ');
  85.     $table->attach($dir_label, 0, 1, 0, 1);
  86.     $prefix_label = &new GtkLabel('File Prefix: ');
  87.     $table->attach($prefix_label, 0, 1, 1, 2);
  88.     $dir_entry = &new GtkEntry();
  89.     $table->attach($dir_entry, 1, 2, 0, 1);
  90.     $prefix_entry = &new GtkEntry();
  91.     $table->attach($prefix_entry, 1, 2, 1, 2);
  92.     $check = &new GtkCheckButton('Backup Directory?');
  93.     $check->set_active(TRUE);
  94.     $table->attach($check, 1, 2, 2, 3);
  95.     $button = &new GtkButton('Rename Files');
  96.     $button->connect('clicked','back_up');
  97.     $table->attach($button, 0, 2, 3, 4);
  98.     $window->show_all();
  99.     Gtk::main();
  100. ?>