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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Interpreting spaces in a file name 1

Status
Not open for further replies.

MonsterAar

Programmer
Aug 16, 2006
24
AU
Code so far:

#!/usr/bin/perl -w beeps.

#loader.pl
#Luke Aaron
#Start: 29/01/08
#Build: A 29/01/08

#Script to act as a script loader to be located in C:\Documents and Settings\USER in order to easily launch scripts from the command line
#located in C:\Documents and Settings\USER\My Documents\My Programs without having to first use CD
#Yes, it's lazy.

print "\nScript: ";
$input = <STDIN>;
chomp($input);
$input =~ tr/A-Z/a-z/;

#check for quit
if($input eq 'exit') {
exit();
};

#create full path
#be sure to change USER in path if need be
@parts = ("C:\\Documents and Settings\\Luke Aaron\\My Documents\\My Programs\\", $input, '.pl');
$path = join('', @parts);

#create command
@cmdp = ('perl', $path);
$cmd = join(' ', @cmdp);

#run command
system($cmd);


The problem is, the output I get is:
Can't open perl script "C:\Documents" etc. It seems that it stops looking at the first space. I tried replacing all spaces with %20 but that didn't help: the whole path showed but it didn't interpret the %20s as spaces.

Any help?

Thanks,
Luke
 
Try this:

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$cmd[/blue] = [red]qq{[/red][purple]perl "$path"[/purple][red]}[/red][red];[/red]
- Miller
 
That works, thanks.

can you tell me why my way didn't work?

Thanks,
Luke
 
You need the quotes around the path so the system function sees it as one string. You should also just use forward slashes in the directory path, Windows fully supports forward slashes, but not DOS.

This is not necessary:

Code:
#create full path
#be sure to change USER in path if need be
@parts = ("C:\\Documents and Settings\\Luke Aaron\\My Documents\\My Programs\\", $input, '.pl');
$path = join('', @parts);

#create command
@cmdp = ('perl', $path);
$cmd = join(' ', @cmdp);

#run command
system($cmd);


easier and better written as:

Code:
#create full path
#be sure to change USER in path if need be
$cmd = qq{perl "C:/Documents and Settings/Luke Aaron/My Documents/My Programs/$input.pl"};

#run command
system($cmd);

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top