Help with DOS file names

S

souporpower

This is my code to print the URL's from the browser favorites:

my @dirs = `dir`;
my $f;
foreach $f (@dirs) {
next if ($f =~ /url/);
`cd $f`;
print "Dir=" . $f . "\n";

# if ($f =~ /url/) {
# print "File=" . $f . "\n";
# open(FIN2, $f);
# while(<FIN2>) {
# print $_ if $_ =~ /http/;
# }
# close(FIN2);
# next;
# }
my @files=`dir *.url`;
my $f2;
foreach $f2 (@files) {
#chop $f2;
#chop $f2;
$f2 =~ s/\x0D//g;
print "AFile=" . "$f" . $f2 . "\n";
#my $file="$f\\$f2";
open(FIN, $f2);
while(<FIN>) {
print $_;
next if ($_ =~ /ico/);
print $_ if $_ =~ /http/ ;
}
close(FIN);
}
#`cd ..`;
}

The problem is each file returned by the DOS dir command has carriage
returns at the beginning of the file name (can't use chop or chomp).
Can someone please help me
out? Tried $f =~ s/^\\n//g;
 
J

Jürgen Exner

This is my code to print the URL's from the browser favorites:

my @dirs = `dir`;

perldoc -f opendir/readdir
perldoc -f glob
my $f;
foreach $f (@dirs) {
next if ($f =~ /url/);
`cd $f`;

What is this `cd` supposed to do? It will start a new process (via
backticks, then change the CWD of that process, and then close the
process. Unless I am missing something that seems pretty pointless to
me.
print "Dir=" . $f . "\n";
my @files=`dir *.url`;

perldoc -f glob

[rest of code snipped]
The problem is each file returned by the DOS dir command has carriage

Well, then don't use that stupid external command but Perl's buildin
utilities aka glob() or opendir/readdir.
returns at the beginning of the file name (can't use chop or chomp).

If you want to you could. Just reverse(), chomp(), reverse(). But I'm
not recommending to do that.
Can someone please help me
out? Tried $f =~ s/^\\n//g;

That would replace a literal backslash followed by a 'n'. Don't escape
the backslash.

Or specify the offending character by its numerical value, using the \x
or \0 notation, see perldoc perlre for details.

jue
 
S

souporpower

This is my code to print the URL's from the browser favorites:
my @dirs = `dir`;

perldoc -f opendir/readdir
perldoc -f glob
my $f;
foreach $f (@dirs) {
       next if ($f =~ /url/);
       `cd $f`;

What is this `cd` supposed to do? It will start a new process (via
backticks, then change the CWD of that process, and then close the
process. Unless I am missing something that seems pretty pointless to
me.
       print "Dir=" . $f . "\n";
       my @files=`dir *.url`;

perldoc -f glob

[rest of code snipped]
The problem is each file returned by the DOS dir command has carriage

Well, then don't use that stupid external command but Perl's buildin
utilities aka glob() or opendir/readdir.
returns at the beginning of the file name (can't use chop or chomp).

If you want to you could. Just reverse(), chomp(), reverse(). But I'm
not recommending to do that.
Can someone please help me
out? Tried $f =~ s/^\\n//g;

That would replace a literal backslash followed by a 'n'. Don't escape
the backslash.

Or specify the offending character by its numerical value, using the \x
or \0 notation, see perldoc perlre for details.

jue

Hi Jue

Modified the code to this:
my @dirs = `dir`;
my $f;
foreach $f (@dirs) {
print "Dir=" . $f . "\n";

opendir(DIR, $f)||die("Can't open $f\n");
@files=readdir(DIR);
}

The program is dying because, I guess, the DOS DIR command is not
returning the full path of the dir.
How can I get the full path of the dir?

Thanks
 
J

Jürgen Exner

Modified the code to this:
my @dirs = `dir`;
my $f;
foreach $f (@dirs) {
print "Dir=" . $f . "\n";
opendir(DIR, $f)||die("Can't open $f\n");

It would be even better to include the reason why the command is
failing:
..... or die ("Can't open $f because $!\n");
@files=readdir(DIR);
}

The program is dying because, I guess, the DOS DIR command is not
returning the full path of the dir.

More important: it doesn't return only directory names. It returns all
file names and on top of that it returns quite some garbage, too.

When I run it I get something like

Volume in drive C has no label.
Volume Serial Number is BCDD-E0FE

Directory of C:\tmp

18-Dec-08 05:33 <DIR> .
18-Dec-08 05:33 <DIR> ..
11-Dec-08 09:04 69 #t.pl#
20-Jul-07 13:40 366,741 001_ESap.pdf
19-Apr-07 16:25 <DIR> bye2A2B.tmp
05-Feb-08 15:47 <DIR> bye3826.tmp

Well, I don't know about your computer, but I certainly have neither a
directory called "Volume in drive C has no label." nor "05-Feb-08 15:47
How can I get the full path of the dir?

opendir() is quite happy with relative paths and has no problems dealing
with them.

jue
 
S

souporpower

It would be even better to include the reason why the command is
failing:
        ..... or die ("Can't open $f because $!\n");



More important: it doesn't return only directory names. It returns all
file names and on top of that it returns quite some garbage, too.

When I run it I get something like

         Volume in drive C has no label.
         Volume Serial Number is BCDD-E0FE

         Directory of C:\tmp

        18-Dec-08  05:33    <DIR>          .
        18-Dec-08  05:33    <DIR>          ..
        11-Dec-08  09:04                69 #t.pl#
        20-Jul-07  13:40           366,741 001_ESap.pdf
        19-Apr-07  16:25    <DIR>          bye2A2B.tmp
        05-Feb-08  15:47    <DIR>          bye3826.tmp

Well, I don't know about your computer, but I certainly have neither a
directory called "Volume in drive C has no label." nor "05-Feb-08  15:47


opendir() is quite happy with relative paths and has no problems dealing
with them.

jue

Thanks Jue. It works now. The problem was as you stated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top