Monday, April 28, 2025

How to make files from a list in Perl

Some time ago I was in need to create a lot of html files, thousands of them in some 20 dirs. So, no manual work here, of course.

Here is simple Perl script to automate this process. There is some file, list-to-make.txt, in this case, where on every line you have name of future file. Program will read that list, and for every line make empty file using system touch UNIX command.

Please note, if you need file with some extension, than state that extension in list-to-make.txt, example:

  • 1file
  • 2file.txt
  • 3file.html
  • 4file.htm

Source code in Perl:

#!usr/bin/perl

print "****************************************************************\n";
print "Program: massmake.pl, ver:0.1, 11/Dec/2012\n";
print "Autor : Milan Smudja";
print "****************************************************************\n";
#This program will make files listed in list-to-make.txt in same dir 
#where program is stored. 

open SOURCEFILE, "list-to-make.txt" || die "Damn, can\'t read from file$!";
@lines = <SOURCEFILE>;
foreach $line (@lines) {
   system "touch $line" || die "Can\'t use sustem touch... $!";
}

 

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...