By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,660 Members | 1,299 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,660 IT Pros & Developers. It's quick & easy.

logic problem

P: 37
Hello friends,

I have a directory Z with file name Z.txt.
I would like to copy this file Z.txt to 8 new dir with new filenames as follows
dir 1 1.txt
dir 2 2.txt
dir 3 3.txt
dir 4 4.txt
dir 5 5.txt
dir 6 6.txt
dir 7 7.txt
dir 8 8.txt

I would like to then open 1.txt from dir 1 and edit the first 4 lines and it
would be better if the program can ask me what to enter in these lines. I need
to do the same for all the directories

Looking forward to your response.

Regards,
Ramesh
Jul 7 '08 #1
Share this Question
Share on Google+
3 Replies


nithinpes
Expert 100+
P: 410
Please do not post your homework questions here. Let us know what you have tried so far and where you are facing problem, so that someone here can help you.
Jul 7 '08 #2

P: 37
Hi all,

I have tried the following, I know the logic behind it, but i am unable to put it rightly using the Perl format. I have tried the following

#!/usr/bin/perl
open FILE, "z.txt" or die $!;
while (<FILE>) {
print $_;
}
mkdir("1", 0777) || print $!;
mkdir("2", 0777) || print $!;
mkdir("3", 0777) || print $!;
mkdir("4", 0777) || print $!;
mkdir("5", 0777) || print $!;
mkdir("6", 0777) || print $!;
mkdir("7", 0777) || print $!;
mkdir("8", 0777) || print $!;
print " -Done";

exit;

I am have all the new directories with respective names. I want to copy the file z.txt from dir z to the new dir 1 with file name 1.txt , dir 2 with file name 2.txt and so on...
How could I use the copy function here.
Looking forward to your response.

Regards,
Ramesh
Jul 7 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
perl has no copy function. You would use the File::Copy module or simply use the open() function to open and read the file and then use "print" to write it to the new directories.

This is not a logic problem. This is an excersize in learning basic file IO operations or possibly more specifically how to mkdir and copy files. Any perl book/tutorial that covers "File Access" or "File IO" should provide you more than enough information to accomplish this very simple task.
Jul 7 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.