For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2005 > spaces in filenames on winXX









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author spaces in filenames on winXX
Harry Putnam

2005-02-20, 3:55 pm

Scripting in perl on a Windows OS and spaces in file names.

I'm pretty sure this has been covered many times here, and I believe
there are modules and such devoted to the problem but searching on
www.cpan.org
with various search strings hasn't turned up something specifically for
this problem, although I may have missed it if the name isn't clearly
about this problem.

Can anyone point me to right stuff

I plan to write a script that checks for and then deletes massive
numbers of files. I'm sure many will have spaces in the name.

John W. Krahn

2005-02-20, 8:55 pm

Harry Putnam wrote:
> Scripting in perl on a Windows OS and spaces in file names.
>
> I'm pretty sure this has been covered many times here, and I believe
> there are modules and such devoted to the problem but searching on
> www.cpan.org
> with various search strings hasn't turned up something specifically for
> this problem, although I may have missed it if the name isn't clearly
> about this problem.
>
> Can anyone point me to right stuff
>
> I plan to write a script that checks for and then deletes massive
> numbers of files. I'm sure many will have spaces in the name.


There shouldn't be any problems as long as you use opendir/readdir to read
file names from a directory and use the three argument form of open if you
have to open files.


John
--
use Perl;
program
fulfillment
Jenda Krynicky

2005-02-20, 8:55 pm

From: Harry Putnam <reader@newsguy.com>
> Scripting in perl on a Windows OS and spaces in file names.
>
> I'm pretty sure this has been covered many times here, and I believe
> there are modules and such devoted to the problem but searching on
> www.cpan.org
> with various search strings hasn't turned up something specifically
> for this problem, although I may have missed it if the name isn't
> clearly about this problem.


I seriously doubt there are any modules realted to this in any way.
Except maybe Shell.pm.

> I plan to write a script that checks for and then deletes massive
> numbers of files. I'm sure many will have spaces in the name.


Perl generaly doesn't have any problems handling such files, you may
have to enclose the filenames in quotes when starting other
executables, but if you stay within Perl you don't need to care.

Try to write the script you need and come back if you run into
problems.

Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

Harry Putnam

2005-02-21, 3:56 am

"Jenda Krynicky" <Jenda@Krynicky.cz> writes:

> Try to write the script you need and come back if you run into
> problems.


This isn't in keeping with Johns point about using opendir/readdir but
I'm not sure I followed that anyway.

Here is an example of problems before even getting to spaces. Doesn't
this mean that some kind of preprocessing must take place?

#!C:\Perl\bin -w

## This is the format that might be handed to perl:
## Here it is inside the script but will really be sent in by 100s of
## ARGV. A windows program will output a large list. Perl will read
## read the filename on each line and take an action

$myfile = "C:\download\my little dir\my other little dir\myfile";
if(-f $myfile){
print "unlinking $myfile\n";
unlink $myfile;
}else{
print "$myfile not found ... moving on;
}

C:\download>perl my.pl
Unrecognized escape \d passed through at my.pl line 8.
Unrecognized escape \m passed through at my.pl line 8.
Unrecognized escape \m passed through at my.pl line 8.
Unrecognized escape \m passed through at my.pl line 8.
C:downloadmy little dirmy other little dirmyfile not found
... moving on

Charles K. Clarkson

2005-02-21, 3:56 am

Harry Putnam <> wrote:

: Here is an example of problems before even getting to spaces.
: Doesn't this mean that some kind of preprocessing must take
: place?
:
: #!C:\Perl\bin -w

Shame! Always use strictures (except when mumble, mumble,
mumble).

use strict;


: ## This is the format that might be handed to perl:
: ## Here it is inside the script but will really be sent in by
: 100s of ## ARGV. A windows program will output a large list.
: Perl will read ## read the filename on each line and take an
: action
:
: $myfile = "C:\download\my little dir\my other little dir\myfile";

You have three choices.


Escape the '\'.

my $file = "C:\\download\\my little dir\\my other little dir\\myfile';


Use single quotes.

my $file = 'C:\download\my little dir\my other little dir\myfile';


Use Perl's path separator ('/').

my $file = 'C:/download/my little dir/my other little dir/myfile';


HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328


Harry Putnam

2005-02-21, 3:57 pm

"Charles K. Clarkson" <cclarkson@htcomp.net> writes:
[...]
> You have three choices.

[...] snipped techniques

So back to my original question:
Is there a module or something that takes care of that pre
processing for me?


Charles K. Clarkson

2005-02-21, 3:57 pm

Harry Putnam <> wrote:

: So back to my original question:
: Is there a module or something that takes care of that pre
: processing for me?

There is no pre-processing. I use this same idiom on
either platform. What do you mean by pre-processing?


my $file = 'foo/bar/baz/no pre-processing.txt';
open FH, $file or die qq(Cannot open "$file": $!);

close FH;


HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328




Jean-Sébastien Guay

2005-02-21, 3:57 pm

Harry,

>[...] snipped techniques
>
>So back to my original question:
> Is there a module or something that takes care of that pre
> processing for me?
>
>


I think you misunderstood. The problem with your script is not the
spaces in the string. It's that you used a single backslash in
double-quoted string. Whether you do that when trying to specify a
filename or not doesn't matter. A single backslash in a double-quoted
string is always interpreted as the beginning of an escape sequence.

For example, \n is newline. But \d and \m don't exist (as escape
sequences), which is why perl is complaining. (notice you have
C:\download\my... in your string)

So, as Charles K. Clarkson said, if you want to keep a double-quoted
string, double your backslashes. (that tells Perl 'This is really meant
to be a backslash and not the beginning of an escape sequence.') Or, use
slashes instead of backslashes. That works great. Or you can also use a
single-quoted string instead of double-quotes, which means Perl won't
try to interpolate anything in the string.

Once you do that, it will work fine.

J-S

--
________________________________________
___
Jean-Sébastien Guay jean_seb@videotron.ca
http://whitestar02.webhop.org/



--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 266.2.0 - Release Date: 2005/02/21

thundergnat

2005-02-23, 3:56 am

Harry Putnam wrote:
> "Charles K. Clarkson" <cclarkson@htcomp.net> writes:
> [...]
>
>
> [...] snipped techniques
>
> So back to my original question:
> Is there a module or something that takes care of that pre
> processing for me?
>
>


As other posters have pointed out, perl can deal with spaces in
file names just fine. For the most part, this isn't really
necessary.

IF, however, you are trying to pass filenames to the command.com
command interpreter for execution under Win95, Win98 or WinME,
you WILL need to "DOSify" the filenames first. (Command.exe for
Win2K and WinXP does't have this problem, though it won't hurt
it either.)

In that case, look into using the Win32::GetShortPathName() method.


****************************************
************************

$filename ='C:\Program Files\Internet Explorer\Connection
Wizard\inetwiz.exe';

$filename = Win32::GetShortPathName($filename);

print $filename;

#system $filename;

****************************************
************************

Really though, that is pretty much the ONLY situation where it
would be necessary.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com