Daniel
Daniel

Reputation: 1357

Why does Perl complain about "Unsuccessful stat on filename containing newline"?

I am getting an error I do not understand. I am using File:Find to recurse a fylesystem on Windows using Activestate Perl 5.8.8 and trying to stat $File::Find::name; so I am not stat-ing a filename got from a text file scanning requiring chomp-ing or newline removing. I was unable to get file modification time, the mtime in:

my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($File::Find::name);

so trying a

-s $File::Find::name 

gives me the error:

Unsuccessful stat on filename containing newline

A typical file name found is F01-01-10 Num 0-00000.pdf but I get the same error even renaming in E02.pdf

Upvotes: 6

Views: 14078

Answers (4)

Ken
Ken

Reputation: 1

Yep, just ran into this myself with a hand built scalar. Scratched my head a bit until I realized I had included a time/date stamp in the filename. Chomp the date command and voila, problem resolved.

Upvotes: 0

Mesa
Mesa

Reputation: 1

Ran into this same problem in my script and found my mistake to be when i created the file i tagged a date stamp to the end and forgot to chomp my $DATE variable before adding it to the file name.

Upvotes: 0

Gayathri
Gayathri

Reputation: 1

Even i got the same error while trying to delete a folder. i used chomp before using rmtree command. That solved my problem.

Upvotes: 0

rjh
rjh

Reputation: 50284

According to perldiag if any file operation fails and the filename happens to contain a newline character, the warning "Unsuccessful on filename containing newline" will be emitted.

The assumption is that, as you say, the filename has come from standard input or similar, and the user has forgotten to chomp the newline away. You might want to pass the string through chomp anyway, just to see if it works.

There is some evidence that &CORE::stat mtime might be broken with some combinations of OS patchlevel and ActiveState Perl versions - a suggested workaround is to use the File::stat module like so:

my $sb = stat($File::Find::name);
my $mtime = scalar localtime $sb->mtime;

...you might find File::stat's object representation to be more convenient than the list returned by CORE::stat.

Upvotes: 13

Related Questions