Getting Windows File Attributes


© Philip Yuson

Who is this for
This article is for programmers who need to get file attributes under a Windows operating System

What you need to know

  • Basic Windows commands

  • Basic Perl programming

Introduction
Sometime ago, I needed to compare two directories and compare the timestamp of the files in these directories. This was fairly easy. However, the difficult part was to recurse to files within subdirectories.

On a Linux/Unix machine, you can do a stat in Perl and check for the directory bit in the file mode item returned from stat.

But this is Windows so stat does not return any information on whether the name is a directory or a file. What did I do?

Option 1: The Quick Way
I have to confess that I cheated. I did this:

open (POOL, "dir $pool |") || die "Cannot open $pool\n";
while () {

if ($pool =~ m/<DIR>/gi) { processDir() }

}


This is the simplest way because it uses the DIR command in DOS and determines if the name is a directory or not based on the string <DIR> that is displayed by the command.

Option 2: The Cleaner Way

The cleaner way is to use the Win32::File module. This module has two methods: GetAttributes and SetAttributes. It also has several constants that you can use to determine the attributes of a given file.

The format of the commands are:

GetAttributes($filename, $resultattr);

SetAttributes($filename, $attrtoset);

Each bit of the resulting byte ($resultattr) from GetAttributes identifies an attribute. To determine the attribute of a file, you need to check if the bit is a 1. You can use these constants to simplify things for you.

ARCHIVE
COMPRESSED
DIRECTORY
HIDDEN
NORMAL
OFFLINE
READONLY
SYSTEM
TEMPORARY

This is a sample routine to show how this works:

Go To Page: 1 2


The copyright of the article Getting Windows File Attributes in Perl is owned by . Permission to republish Getting Windows File Attributes in print or online must be granted by the author in writing.

Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo


Here's the follow-up discussion on this article: View all related messages

2.   Jan 24, 2004 1:39 PM
In response to message posted by DigiAvitar:

A response of -1 means that the file does not exist ...

-- posted by pyuson


1.   Nov 24, 2003 1:30 PM
What does a return code of "-1" mean?

After issuing the following command all test are TRUE:

Win32::File::GetAttributes($filename, $attr);
if ($attr & DIRECTORY) { print "$filenam ...


-- posted by DigiAvitar





For a complete listing of article comments, questions, and other discussions related to Philip Yuson's Perl topic, please visit the Discussions page.