130 likes | 153 Views
File and Directory Manipulation. Fig. 11.1 Some file tests. 1 #!/usr/bin/perl. 2 # Fig. 11.2: fig11_02.pl. 3 # A program that uses file tests. The filename test -e determines whether a file exists. 4 .
E N D
1 #!/usr/bin/perl 2 # Fig. 11.2: fig11_02.pl 3 # A program that uses file tests The filename test -e determines whether a file exists. 4 File test -f file test shows whether or not a file is a plain file. Calling a file “plain” means that the file is not a special type of file or listing. 5 use strict; 6 use warnings; File tests –x, -r and –w show whether the file is executable, readable and writable, respectively. 7 8 foreachmy $file ( @ARGV ) { File tests –M and -A return the number of days (or fraction thereof) since the file was last modified or last accessed, respectively. 9 print( "Checking $file: " ); If a file is nonempty, -s returns the size of the file in bytes. 10 11 if ( -e $file ) { # does file exist? 12 print( "$file exists!\n" ); 13 14 if ( -f $file ) { # is the file a plain file? 15 print( "The file $file is:" ); 16 print( " executable" ) if ( -x $file ); # executable? 17 print( " readable" ) if ( -r $file ); # readable? 18 print( " writable" ) if ( -w $file ); # writable? 19 print( "\n" ); 20 print( "It is ", -s $file, " bytes.\n" ); # size 21 my @time = timeconv( -A $file ); # accessed 22 print( "Last accessed at $time[0] days, ", 23 "$time[1] hours, $time[2] minutes ", 24 "and $time[3] seconds.\n" ); 25 @time = timeconv( -M $file ); # modified 26 print( "Last modified at $time[0] days, ", 27 "$time[1] hours, $time[2] minutes, ", 28 "and $time[3] seconds ago.\n" ); 29 }
30 elsif ( -d $file ) { # is it a directory? 31 print( "$file is a directory!\n" ); File test –d shows whether the file is directory. 32 } 33 } 34 else { 35 print( "$file doesn't exist.\n" ); 36 } 37 38 print( "\n" ); 39 } 40 41 sub timeconv 42 { 43 my $time = shift(); 44 my $days = int( $time ); 45 $time = ( $time - $days ) * 24; 46 my $hours = int( $time ); 47 $time = ( $time - $hours ) * 60; 48 my $minutes = int( $time ); 49 $time = ( $time - $minutes ) * 60; 50 my $seconds = int( $time ); 51 return ( $days, $hours, $minutes, $seconds ); 52 }
Checking fig11_02.pl: fig11_02.pl exists! The file fig11_02.pl is: executable readable writable It is 1550 bytes. Last accessed at 0 days, 0 hours, 0 minutes and 0 seconds. Last modified at 0 days, 0 hours, 2 minutes, and 20 seconds ago. Checking /home/pauldeitel: /home/pauldeitel exists! /home/pauldeitel is a directory! Checking file.txt: file.txt exists! The file file.txt is: readable writable It is 51 bytes. Last accessed at 0 days, 2 hours, 40 minutes and 16 seconds. Last modified at 1 days, 17 hours, 39 minutes, and 28 seconds ago. Checking fakefile.txt: fakefile.txt doesn't exist.
1 #!/usr/bin/perl 2 # Fig. 11.5: fig11_05.pl Checks if the file exists. 3 # Renaming a file before accidental deletion 4 5 use warnings; 6 use strict; If the user enters no, the contents of file.txt are copied to file.old. 7 8 if ( -e 'file.txt' ) { 9 print( "Do you want to write over file.txt? (yes or no): " ); 10 chomp( my $response = <STDIN> ); The contents of file.txt are overwritten. 11 rename( 'file.txt', 'file.old' ) 12 or die( "Error renaming : $!" ) 13 if ( $response eq 'no' ); 14 } 15 16 open( FILE, ">file.txt" ) or die( "Error opening: $!" ); 17 print( FILE "A copy of file.txt is saved in file.old.\n" ); 18 close( FILE ) or die( "Cannot close: $!" ); Do you want to write over file.txt? (yes or no): no
file.txt before program executes: This is the original text from file.txt. file.txt after program executes: A copy of file.txt is saved in file.old. file.old after program executes: This is the original text from file.txt.
1 #!usr/bin/perl 2 # Fig. 11.9: fig11_09.pl Checks if the file is a plain file. 3 # Deleting a file with unlink Prompts the user for a file to be deleted. 4 5 use strict; 6 use warnings; Function unlink deletes a list of files and returns the number of files successfully deleted. 7 8 print( "Input a file you want deleted: " ); 9 chomp( my $file = <STDIN> ); 10 11 if ( -f $file && unlink( $file ) ) { 12 print( "The file was successfully deleted.\n" ); 13 } 14 else { 15 print( "It was not deleted: $!" ); 16 } Input a file you want deleted: file.old The file was successfully deleted. Input a file you want deleted: doesnotexist.txt It was not deleted: No such file or directory
1 #!perl 2 # Fig. 11.10: fig11_10.pl 3 # Website update-page creator. The program begins by declaring four variables with the keyword our, so that these values can be accessed by the user-defined functions that follow. 4 We declare $root to hold the value "I:/Apache/cgi-bin/" and use chdir to change the working directory to this value. 5 use strict; 6 use warnings; 7 use CGI qw( :standard ); Start the HTML page. Call to the function search. 8 End the HTML page. 9 our $indent = "|" . ( " " x 5 ); Function opendir is called to create a directory handle for the current directory to search. Function search begins by assigning the two arguments to $directory and $offset. In this case, $directory is "", and $offset is 1. 10 our $root = "I:/Apache/cgi-bin/"; 11 chdir( $root ); 12 our @colors = qw( red orange green ); 13 our @fileTypes = qw( html perl dir ); 14 print( header(), start_html( -title => 'Update Page' ), 15 '<font size = "+1">' ); 16 print( "$root:<br/>\n" ); 17 search( "", 1 ); 18 print( "</font>" ); 19 print( end_html() ); 20 21 sub search 22 { 23 my ( $directory, $offset ) = @_; 24 opendir( DIR, $root . $directory ) 25 or die( "Error opening: $!" ); 26
27 foreach ( readdir( DIR ) ) { 28 my $file = $directory . $_; The variable $file is assigned the directory in which we started ($directory) concatenated with the current value returned from readdir. 29 printFile( 0, $file, $_, $offset ) if ( m/\.html/ ); If the current file is a directory, then we want to iterate through that directory as well using recursion. Calls function printFile if the file is an HTML file. Calls function printFile if the file is a Perl program file. The foreach structure iterates through all the file and directory names in the current directory. 30 printFile( 1, $file, $_, $offset ) if ( m/\.pl/ ); Recursive call to search. In this call to search, we provide the new directory name ($file) concatenated with a path separator. The directory name is printed by calling printFile. 31 Prints the $indent in a for structure based on $offset. 32 if ( -d $file && /[A-Za-z]/ ) { If the file was modified in the last seven days (and thus also in the last 30 days), both "brand " and "new!" are printed after the file. If the file was modified in the last 30 days, only the word "new!" is printed. Function printFile begins by reading in its arguments and using the filename with the root directory to assign the full path name of $file to $full. 33 printFile( 2, $file, $_ . '/', $offset ); The current color is also determined based on the type of file or directory being printed. 34 search( $file . '/', $offset + 1 ); 35 print( "$indent" ) for ( 2 .. $offset ); The filename is printed. Sets the font color based on the previously set value $color and prints the type of file to be printed. 36 print( br(), "\n" ); 37 next; 38 } 39 } 40 } 41 42 sub printFile 43 { 44 my ( $type, $file, $name, $offset ) = @_; 45 my $full = $root . $file; 46 print( "$indent" ) for ( 2 .. $offset ); 47 print( "|----" ); 48 my $color = $colors[ $type ]; 49 my $extension = $fileTypes[ $type ]; 50 print( "<font color = \"$color\">$extension: " ); 51 print( "$_</font>\n" ); 52 print( em( strong( "brand " ) ) ) if ( -M $full < 7 ); 53 print( strong( "new!" ), "\n" ) if ( -M $full < 30 ); 54 print( br(), "\n" ); 55 }