bgi has asked for the wisdom of the Perl Monks concerning the following question:

hi, im writing a backup script on windows...so I strated to check how I can easily get a files timestamp and the extensions or txpe of the file...

For the timpestamp I use stat(), for the filetype I use a workaround with the system command, the FOR loop and write the ouptput to a textfile...

And that's my problem...there must be a more easier way to geht just the type/extension of a file?????

I thought about something with regex, but I'm not sure if it works also on computers where the file extensions are not shown (there is a option which is named like "don't show extensions of known files") I really hope you could help me..!

Reagrds Ben

Replies are listed 'Best First'.
Re: file managing under windows
by jethro (Monsignor) on Jun 20, 2009 at 13:58 UTC

    Not showing the file extensions is a function of the graphical user interface (GUI) and has no influence on what perl (or other) scripts see. A script will even see some files that are not shown at all in the GUI

    You might use the module File::Basename to extract the suffix

Re: file managing under windows
by generator (Pilgrim) on Jun 21, 2009 at 06:48 UTC
    I do this sort of thing but more in a "cleanup" capacity rather than "backup." The following code simply looks for any files in the current directory with an extension of .xcp and deletes any where the modification date is greater than 10 days old.

    #!C:/perl/bin/perl.exe; use strict; use warnings; use Cwd; # delete .xcp files older than 10 days #Identify my current directory and record it to $cdir my $cdir = getdcwd(); #create an array of all files with an exntesion of .xcp my @files = glob("*.xcp"); foreach my $file (@files) { # for each element of the array delete if mod date is greater than 10 +days unlink "$cdir\\$file" if -M $file > 10; }
    A quick google search for "perl file attributes" presented This link to a list of file characteristics (along with many others) you can use as evaluation criteria.

    I hope this helps.

    <><

    generator

Re: file managing under windows
by cdarke (Prior) on Jun 21, 2009 at 10:33 UTC
    The type/extension is held in HKEY_CLASSES_ROOT in the registry. Win32::FetchCommand may help, it does not return the file type, but the name of the program associated with it.

    If asked nicely, the author might make a modification to provide the file type. Or msg me.