M626
Programmer
- Mar 13, 2002
- 299
how can i rename all the files in a folder to the folder name and append _1 _2 _3 and so on for each additional file in the folder?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
use Win32::GUI;
use File::Copy;
my ($dos) = Win32::GUI::GetPerlWindow();
Win32::GUI::Hide($dos);
$Menu = Win32::GUI::MakeMenu(
"&File" => "File",
" > E&xit" => { -name => "File_Exit", -onClick => sub { -1; } },
"&Help" => "Help",
" > General Help" => "GenHelp",
" > -" => 0,
" > About FileWrangler" => "About",
);
my $main = Win32::GUI::Window->new(
-name => 'Main',
-text => 'FileWrangler',
-width => 500,
-height => 325,
-menu => $Menu,
);
$main->Center();
$main->AddLabel(
-text => "Source List:",
-pos => [220,15],
);
my $CSVfield = $main->AddTextfield(
-name => "CSVfield",
-left => 75,
-top => 35,
-height => 20,
-width => 300,
-text => "",
);
my $CSVButton = $main->AddButton(
-name => "CSVButton",
-text => "Browse",
-pos => [390,35],
);
$main->AddLabel(
-text => "File Destination:",
-pos => [210,75],
);
my $Destfield = $main->AddTextfield(
-name => "Destfield",
-left => 75,
-top => 95,
-height => 20,
-width => 300,
-text => "",
);
my $DestButton = $main->AddButton(
-name => "DestButton",
-text => "Browse",
-pos => [390,95],
);
$main->AddLabel(
-text => "Rename files to:",
-pos => [210,135],
);
my $PreLable = $main->AddLabel(
-text => "Prefix",
-pos => [125,158],
);
my $PreField = $main->AddTextfield(
-name => "PreField",
-left => 155,
-top => 155,
-height => 20,
-width => 75,
-text => "",
);
my $NumLable = $main->AddLabel(
-text => "Starting Number",
-pos => [235,158],
);
my $NumField = $main->AddTextfield(
-name => "NumField",
-left => 315,
-top => 155,
-height => 20,
-width => 50,
-text => "1",
-number => 1,
);
my $HelpLable = $main->AddLabel(
-text => "If Prefix = \'Foobar\' and Number = \'42\' then Filename will be \'Foobar42\'",
-pos => [75,180],
);
$main->AddLabel(
-text => "Files will retain their original extension.",
-pos => [160,200],
);
my $ProcStat = $main->AddLabel(
-name => "ProcStat",
-text => " ",
-pos => [400, 255],
);
my $GoButton = $main->AddButton(
-name => "GoButton",
-text => "Start Copy",
-pos => [215,235],
);
##################### Subroutines ############################################
sub copyDir_Change {
$searchdir = ($dirfield->Text);
}
sub CSVfield_Change {
$CSVfile = ($CSVfield->Text);
}
sub CSVButton_Click {
my $CSVfile = $main->BrowseForFolder(
-title => "Select CSV File - FileWrangler",
-root => (0x0011),
-owner => "$main",
-includefiles => 1,
);
if ($CSVfile ne '') {
$CSVfield->Text("$CSVfile");
}
}
sub DestButton_Click {
my $destdir = $main->BrowseForFolder(
-title => "Select Destination Directory - FileWrangler",
-root => (0x0011),
-owner => "$main",
);
if ($destdir ne '') {
$Destfield->Text("$destdir");
}
}
#################################### Copy Routine ###################################
sub GoButton_Click {
$pre = $PreField->Text;
$num = $NumField->Text;
$dest = $Destfield->Text;
if ($dest eq "") {
die "No Destination Directory!\n";
}
if (substr($dest, -1) ne "\\") {
$dest = "$dest\\";
}
my $csvloc = $CSVfield->Text;
open(CSV,"$csvloc") || die "Can't open Source List!\n";
@FileList = <CSV>;
close(CSV);
$ifile = 'index.txt';
$index = "$dest$ifile";
open(IDX, ">$index");
$ProcStat->Text("Copying");
foreach $CopyFileRaw(@FileList) {
@array = split(/\\/, $CopyFileRaw);
$filename = "@array[$#array]";
$CopyFileDest = "$dest$filename";
&Check_Rename;
}
close IDX;
$ProcStat->Text("Finished");
}
sub Check_Rename {
@array = split(/\\/, $CopyFileDest);
for (my $i = 0; $i < $#array; $i++) {
$iter = @array[$i];
$path = "$path\\$iter";
}
$CleanPath = substr($path, 1);
$path = "";
$Fname = "@array[$#array]";
chomp ($Fname);
@FileArray = split(/^(.*?)\.?([^\.]*)$/, $Fname);
if ($NumField->Text eq "") {
die "No Starting File Number!\n";
}
$NewName = "\\$pre$num\.@FileArray[2]";
$New_Name = "$pre$num\.@FileArray[2]";
$num++;
$CopyFileDest = "$CleanPath$NewName";
chomp ($CopyFileRaw);
chomp ($CopyFileDest);
copy ("$CopyFileRaw", "$CopyFileDest") || die "Couldn't copy file $CopyFileRaw\n";
print IDX "$New_Name was $CopyFileRaw\n";
}
sub Main_Terminate {
return -1;
}
sub Exit {
return -1;
}
########################################### About Routines ##################
my $about = Win32::GUI::DialogBox->new(
-name => 'about',
-text => 'FileWrangler.pl',
-width => 300,
-height => 280,
-parent => $main,
);
$about->Center();
$about->AddLabel(
-height => 200,
-width => 280,
-top => 15,
-align => "center",
-text => "FileWrangler.pl\nVersion 1.0\nby\nMichael Davis\nmikedavis\@ci.tulsa.ok.us\n\nWritten with ActiveState Perl for Win32\n(ActivePerl 5.8.7.815)\n[URL unfurl="true"]www.activestate.com\n\nUses[/URL] Win32::GUI - not portable to X.\n\n Compiled with Perl2exe v 8.70\n[URL unfurl="true"]www.indigostar.com",[/URL]
);
$about->AddButton(
-name => 'AboutOK',
-text => 'OK',
-default => 1, # Give button darker border
-ok => 1, # press 'Return' to click this button
-height => 30,
-width => 50,
-left => 120,
-top => 210,
);
sub About_Click {
$about->DoModal();
}
sub about_Terminate {
return -1;
}
sub AboutOK_Click {
return -1;
}
########################## Help Routine ######################
my $genhelp = Win32::GUI::DialogBox->new(
-name => 'genhelp',
-text => 'FileWrangler Help',
-width => 300,
-height => 380,
-parent => $main,
);
$genhelp->Center();
$genhelp->AddLabel(
-height => 300,
-width => 280,
-top => 15,
-align => "center",
-text => "FileWrangler is a routine that copies and renames files.It was written to assist in responding to FOI requests.\n\nFileWrangler reads a list of files to process called the Source List. This file should contain the complete path and filename of each file to process, one entry per line.\n\nAfter setting the File Destination directory, you can set a prefix and a starting number for the filenames you wish to end up with. All files will retain their original extension.\n\nFor example, if there are two files in the File List named yada.txt and test.doc, setting the prefix to Foobar and the starting number to 42 will result in those files being copied and renamed to Foobar42.txt and Foobar43.doc.\n\nAn index file (index.txt) is created in the Destination directory that lists the original path and filename and what the resulting filename is.\n\nThe lower right-hand corner of the window will indicate when the copy process has finished.\n",
);
$genhelp->AddButton(
-name => 'genhelpOK',
-text => 'OK',
-default => 1, # Give button darker border
-ok => 1, # press 'Return' to click this button
-height => 30,
-width => 50,
-left => 120,
-top => 310,
);
sub GenHelp_Click {
$genhelp->DoModal();
}
sub GenHelp_Terminate {
return -1;
}
sub genhelpOK_Click {
return -1;
}
######################### show main window ################################
$main->Show();
Win32::GUI::Dialog();
Win32::GUI::Show($dos);