Gossamer Forum
Home : General : Perl Programming :

Script for creating files

Quote Reply
Script for creating files
Hi,

I need to create about 9000 .txt files.......and they all have the same info in them, with the exception of a number. For example 1.txt has a line of code with the number 1 in it, 2.txt has the same line of code with the number two in it, and so on! I'm currently doing this manually, but it's getting rather tiresum......anyway, does anyone know of an easier (automated) solution to assist with what I'm doing? Preferably a script of some sort, that I could just run, and the files will be created. I appreciate your input.
Thanks.

Quote Reply
Re: Script for creating files In reply to
Here a short script that took a few secs to write. Hope it's what your looking for.

Code:
#!/usr/local/bin/perl

$i = 1;
$num_files = 9000;
$dir = "path to folder to create files in";

while ($i <= $num_files) {
open (FILE, ">>$dir/$i.txt");
print FILE "$i\tconstant stuff here\n";
close (FILE);
$i += 1;
}
--Drew
Quote Reply
Re: Script for creating files In reply to
 
To think.....I was going to do these manually!! Drew, your the man, thanks alot, works like a charm! Although, I did notice one additional thing I need to happen..... if it's no trouble? It won't work if there's additional characters....so I guess my question is, how can I allow other characters in the "constant stuff area" -for example something in quotes? Or stuff within < >?

print FILE "$i\"constant <stuff> here"\n";

Thank you very much, this will save me a ton of time!

Quote Reply
Re: Script for creating files In reply to
You'd have to escape the quotes and Perl things with a backslash (ie: \"Hello\") or use qq so you don't have to worry about it (in most cases).
Code:
print FILE qq|$i\t"Have a nice day" --<i>Anonymous</i>\n|;
--Drew
Quote Reply
Re: Script for creating files In reply to
Hmm....I thought I tried that before asking, and it didn't look like it was working! Oh well, thank you very much, it works great now. I really appreciate your help Drew.