Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Testing for a dir on Windows XP

Status
Not open for further replies.

readey

Technical User
Feb 12, 2007
5
GB
Hi, I'm using Activestae perl on a WinXP box. Can anyone tell me the best way to test if a file is a directory since -d does not seem to work.

Thanks :)
 
Activestate user here too - works here. Two things:
1 - Make sure you're not doing something like:
Code:
    opendir DH, $some_path ; # Verification skiped for example
    foreach $name (readdir DH) {
       if (-d $name) then {
          # do this
       } else {
          # do that
       };
    };
[\code]
You want to test against "$some_path/$name"

2 - Careful if you're trying*:
[code]
   open(FH, "dir |");
   while ($line = <FH>) {
      if (-d $line) {
         # actions w/ dir
      };
   };
* or pulling anything from direct OS output

If you're doing a simple parse of the input line, be sure to clean up the end of the line (eg. chomp) as you may be trying to match against "some_file_name" . "\n" where the "some_file_name" may indeed be a file, but the same name with a "\n" character attached to the end does not exist as a file.
 
-d should work on XP with activestate perl, I suspect the problem is associated with not using the file test operator correctly as outlined above.

Side notes:

Code:
if (-d $name)[b]then[/b] {

there is no "then" in perl. There are also semi-colons in the above code that should not be there, like at the end of the "if", "else", and "foreach" blocks.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
if ... then - What can I say. Sometimes when you're multi-lingual, words from other languages sneak in.

As for the additional ;'s - I disagree with the exact wording of "shouldn't be there". I'll accept "don't need to be there". Before I allow myself to stumble into a debate on this topic I'll cut myself off as I'm sure there are other forms to handle this.
 
perlfunc#readdir said:
If you're planning to filetest the return values out of a readdir, you'd better prepend the directory in question.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
I assumed the "then" was there for the reason you posted, snuck in from KSH or AWK or something else. The semi-colons should not be at the end of the blocks. In your code they will do no harm, but in a larger program having errant semi-colons could lead to hard to find bugs. You will not see those semi-colons in any perl book or tutorial or in the code examples that come with perl. Not a big deal, I hope I did not offend you by mentioning it.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
well my original script had the line :-

foreach my $file (@files) {

next if -d $file;

etc, etc;

}

this did not seem to work ,however taking everyone's advice I changed the line to;

-d $file && next;

This seems to work!!!!
 
The original style is less arcane and probably would have worked with parentheses
Perl:
next if [red]([/red]-d $file[red])[/red];
But unless you followed all the warnings about sticking the path on the front, it will break if you move anywhere outside of the current working directory.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top