Home > Archive > PERL Beginners > October 2005 > rename and move multiple files
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
rename and move multiple files
|
|
| Perlwannabe 2005-10-24, 3:55 am |
| I have a file I download everyday, let's call it "output.txt." The file
"output.txt" is saved in a directory by date, for example 10012005 (for Oct.
1, 2005). I have a years worth of output files in my c:\ drive. Now I have
to rename each output file so that I can copy all of the output to a single
directory. It's a nightmare to do manually. I really don't care what the
files are named as long as each file gets a unique name. Imaging this:
C:\01012005
C:\01022005
C:\01032005
C:\01042005
... . .
Now, each of those directories has a file in it called "output.txt." I want
to get everyone of those "output.txt" and copy it to a single directory
(call it c:\renoutput) and each output will have a unique name. So when I
do a "dir" of C:\RENOUTPUT is looks like:
output1.txt
output2.txt
output3.txt
output4.txt
... . .
So I suppose I want to do a locate, rename, move. 1) locate all
"output.txt" files on the hard drive; 2) rename each "output.txt" to
something unique, and 3) move each renamed file from its original location
to a single directory.
I have tried a few ways with no success. Thanks for the help.
| |
| John W. Krahn 2005-10-24, 3:55 am |
| perlwannabe wrote:
> I have a file I download everyday, let's call it "output.txt." The file
> "output.txt" is saved in a directory by date, for example 10012005 (for Oct.
> 1, 2005). I have a years worth of output files in my c:\ drive. Now I have
> to rename each output file so that I can copy all of the output to a single
> directory. It's a nightmare to do manually. I really don't care what the
> files are named as long as each file gets a unique name. Imaging this:
>
> C:\01012005
> C:\01022005
> C:\01032005
> C:\01042005
> .. . .
>
> Now, each of those directories has a file in it called "output.txt." I want
> to get everyone of those "output.txt" and copy it to a single directory
> (call it c:\renoutput) and each output will have a unique name. So when I
> do a "dir" of C:\RENOUTPUT is looks like:
>
> output1.txt
> output2.txt
> output3.txt
> output4.txt
> .. . .
>
> So I suppose I want to do a locate, rename, move. 1) locate all
> "output.txt" files on the hard drive; 2) rename each "output.txt" to
> something unique, and 3) move each renamed file from its original location
> to a single directory.
>
> I have tried a few ways with no success. Thanks for the help.
UNTESTED (sorry I don't use Windows):
my $dir = '/renoutput';
mkdir $dir or die "mkdir '$dir' $!";
my $count = 1;
for my $file ( glob '/*/output.txt' ) {
rename $file, "$dir/output" . $count++ . '.txt'
or die "Cannot rename '$file' to '$dir/output$count.txt' $!";
}
John
--
use Perl;
program
fulfillment
| |
| Elie De Brauwer 2005-10-24, 3:55 am |
| perlwannabe wrote:
> I have a file I download everyday, let's call it "output.txt." The file
> "output.txt" is saved in a directory by date, for example 10012005 (for Oct.
> 1, 2005). I have a years worth of output files in my c:\ drive. Now I have
> to rename each output file so that I can copy all of the output to a single
> directory. It's a nightmare to do manually. I really don't care what the
> files are named as long as each file gets a unique name. Imaging this:
>
> C:\01012005
> C:\01022005
> C:\01032005
> C:\01042005
> .. . .
>
> Now, each of those directories has a file in it called "output.txt." I want
> to get everyone of those "output.txt" and copy it to a single directory
> (call it c:\renoutput) and each output will have a unique name. So when I
> do a "dir" of C:\RENOUTPUT is looks like:
>
> output1.txt
> output2.txt
> output3.txt
> output4.txt
> .. . .
>
> So I suppose I want to do a locate, rename, move. 1) locate all
> "output.txt" files on the hard drive; 2) rename each "output.txt" to
> something unique, and 3) move each renamed file from its original location
> to a single directory.
>
> I have tried a few ways with no success. Thanks for the help.
>
It's hard to imagine that you spend multiple days looking at such an
easy problem and can't come up with a single line of code to show the
list. To get you started here's a solution of your problem in bash (you
do have bash installed on your windows box ?)
Create a test environment:
helios@neurotic:~/vuilbak$ for i in 1 2 3 4 5 6 7; do mkdir $i; touch
$i/output.txt; done
Create directory for output
helios@neurotic:~/vuilbak$ mkdir result;
Use the following bash oneliner:
helios@neurotic:~/vuilbak$ (( i=0 )); for j in `find . -name
"output.txt"`; do cp $j result/output$i.txt ; (( i++ )); done
Verify result:
helios@neurotic:~/vuilbak$ ls result/
output0.txt output2.txt output4.txt output6.txt
output1.txt output3.txt output5.txt
gr.
E.
| |
| Timothy Johnson 2005-10-24, 3:55 am |
| RGlkIHRoaXMgdHVybiBpbnRvIHRoZSBiYXNoIGxp
c3Qgd2hpbGUg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looking at such an
> easy problem and can't come up with a single line of code to show the
> list. To get you started here's a solution of your problem in bash (you
> do have bash installed on your windows box ?)
WOW! I didn't "show code" because I have about 4 or 5 different concepts
going, none of which worked. However, I suppose this "easy problem" was
too complex for my simple mind. If you look at the archives, you will
notice that I seldom request help, and am always courteous and thankful
for the assistance. Perhaps with a simple mind comes common courtesy, and
with a complex mind, such as yours, comes demeaning remarks and rampant
superiority. I'll take the simple mind any day.
>
> Create a test environment:
> helios@neurotic:~/vuilbak$ for i in 1 2 3 4 5 6 7; do mkdir $i; touch
<<SNIP>>
| |
| Perlwannabe 2005-10-24, 6:56 pm |
| <<SNIP>>
>
> UNTESTED (sorry I don't use Windows):
>
>
> my $dir = '/renoutput';
> mkdir $dir or die "mkdir '$dir' $!";
>
> my $count = 1;
>
> for my $file ( glob '/*/output.txt' ) {
> rename $file, "$dir/output" . $count++ . '.txt'
> or die "Cannot rename '$file' to '$dir/output$count.txt' $!";
> }
>
>
ARRGHHHH!!!! OMG!!! I had code that was very close to this... but I was
using the wildcard (*) in an attempt to rename the file. Basically, where
you have " . . . "$count++ . '.txt' ..." I was using "$count++ .
'outp*.txt'. . ." When I removed the wildcard, everything worked
perfectly. I now have a directory named c:/renoutput with 522 files
numbered output1 through output 522.
Thanks a million. Worked great and my project is close to getting done.
|
|
|
|
|