Chapter 11 file and directory manipulation
This presentation is the property of its rightful owner.
Sponsored Links
1 / 13

Chapter 11 – File and Directory Manipulation PowerPoint PPT Presentation


  • 56 Views
  • Uploaded on
  • Presentation posted in: General

Chapter 11 – File and Directory Manipulation. Outline 11.1Introduction 11.2File Tests and sysopen 11.3Permissions 11.4File Manipulation 11.5Hard Links and Symbolic Links 11.6File Globbing 11.7Directory Handles and Manipulation 11.8Example: Web Site Recent-Update Page.

Download Presentation

Chapter 11 – File and Directory Manipulation

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Chapter 11 file and directory manipulation

Chapter 11 – File and Directory Manipulation

Outline11.1Introduction11.2File Tests and sysopen11.3Permissions11.4File Manipulation11.5Hard Links and Symbolic Links11.6File Globbing11.7Directory Handles and Manipulation11.8Example: Web Site Recent-Update Page


Chapter 11 file and directory manipulation

Fig. 11.1 Some file tests.


Chapter 11 file and directory manipulation

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.

5use strict;

6use warnings;

File tests –x, -r and –w show whether the file is executable, readable and writable, respectively.

7

8foreachmy $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

11if ( -e $file ) { # does file exist?

12 print( "$file exists!\n" );

13

14if ( -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

21my @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 }


Chapter 11 file and directory manipulation

30elsif ( -d $file ) { # is it a directory?

31 print( "$file is a directory!\n" );

File test –d shows whether the file is directory.

32 }

33 }

34else {

35 print( "$file doesn't exist.\n" );

36 }

37

38 print( "\n" );

39}

40

41sub timeconv

42{

43my $time = shift();

44my $days = int( $time );

45 $time = ( $time - $days ) * 24;

46my $hours = int( $time );

47 $time = ( $time - $hours ) * 60;

48my $minutes = int( $time );

49 $time = ( $time - $minutes ) * 60;

50my $seconds = int( $time );

51return ( $days, $hours, $minutes, $seconds );

52}


Chapter 11 file and directory manipulation

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.


Chapter 11 file and directory manipulation

1#!/usr/bin/perl

2# Fig. 11.5: fig11_05.pl

Checks if the file exists.

3# Renaming a file before accidental deletion

4

5use warnings;

6use strict;

If the user enters no, the contents of file.txt are copied to file.old.

7

8if ( -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' )

12or die( "Error renaming : $!" )

13if ( $response eq 'no' );

14}

15

16open( FILE, ">file.txt" ) or die( "Error opening: $!" );

17print( FILE "A copy of file.txt is saved in file.old.\n" );

18close( FILE ) or die( "Cannot close: $!" );

Do you want to write over file.txt? (yes or no): no


Chapter 11 file and directory manipulation

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.


Chapter 11 file and directory manipulation

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

5use strict;

6use warnings;

Function unlink deletes a list of files and returns the number of files successfully deleted.

7

8print( "Input a file you want deleted: " );

9chomp( my $file = <STDIN> );

10

11if ( -f $file && unlink( $file ) ) {

12 print( "The file was successfully deleted.\n" );

13}

14else {

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


Chapter 11 file and directory manipulation

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.

5use strict;

6use warnings;

7use CGI qw( :standard );

Start the HTML page.

Call to the function search.

8

End the HTML page.

9our $indent = "|" . ( "&nbsp;" 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.

10our $root = "I:/Apache/cgi-bin/";

11chdir( $root );

12our @colors = qw( red orange green );

13our @fileTypes = qw( html perl dir );

14print( header(), start_html( -title => 'Update Page' ),

15 '<font size = "+1">' );

16print( "$root:<br/>\n" );

17search( "", 1 );

18print( "</font>" );

19print( end_html() );

20

21sub search

22{

23my ( $directory, $offset ) = @_;

24 opendir( DIR, $root . $directory )

25 or die( "Error opening: $!" );

26


Chapter 11 file and directory manipulation

27foreach ( readdir( DIR ) ) {

28my $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.

32if ( -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" );

37next;

38 }

39 }

40}

41

42sub printFile

43{

44my ( $type, $file, $name, $offset ) = @_;

45my $full = $root . $file;

46 print( "$indent" ) for ( 2 .. $offset );

47 print( "|----" );

48my $color = $colors[ $type ];

49my $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}


  • Login