ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl-win32-users
perl-win32-users
Re: Back-slashes & calling a batch file from perl ???
by James Sluka other posts by this author
Oct 28 2005 9:39AM messages near this date
view in the new Beta List Site
Re: Back-slashes & calling a batch file from perl ??? | Question on exit code of Win32::Process
One more thing to try is to add a trailing space after the directory 
spec, as in;
    system qq{$prog "$dir " $dest}
or
    my $cmd = qq{$prog "$dir "  "$dest"};

I did some quick tests with;
##########Perl code
my $prog = 'c:\windows\desktop\some-batch.bat';
my $dir  = 'c:\windows\desktop\jim';    # no trailing backslash
my $dest = 'thelabel';

# both the directory and batch files do exist
if (-e $prog) {print "prog found\n"} else {print "prog NOT found\n"};
if (-d $dir)  {print "dir  found\n"} else {print "dir  NOT found\n"};

# Version 1, works
system qq{$prog "$dir " $dest}; # quoted $dir in case blanks in path
                                # and note the extra space in "$dir "

# Version 2, works
my $cmd = qq{$prog "$dir "  "$dest"}; # note the extra space in "$dir "
print "\n\nCMD ==", $cmd, "=\n";
system "$cmd";
# end Perl code

### some-batch.bat contains
echo %1
echo %2
dir %1

The perl code above fails if the trailing space after the dir spec is 
omitted. The space can be in the original variable or added when the 
command is created (as I did above).

jim

$Bill Luebkert wrote:

> Michael D Schleif wrote:
>   
> 
> >* On 2005:10:27:15:07:31-0500 I, Michael D Schleif <mds@[...].org>, scribed:
> >    
> >
> >>I have a perl script that calls a batch file (necessary), and passes it
> >>two arguments.  The first argument is a directory name, and the second a
> >>simple label.
> >>
> >>When I used forward-slashes (/) everywhere, the perl script behaves as
> >>expected; but, the batch file refuses to recognize the legitimacy of the
> >>directory name; at least in the context of passing it to the batch file
> >>in a system() call, as I need to parse the exit codes.
> >>
> >>    my $dir = "E:/backup";
> >>
> >>Yes, I test `-d $dir' successfully.  The batch file refuses to accept
> >>$dir while using forward-slashes.  I am using s/// to replace (/) with
> >>any number of (\).  I have tried up to eight (8) back-slashes; but,
> >>everytime the script mis-behaves, and I have not been able to complete
> >>this simple task.
> >>
> >>What am I missing?
> >>
> >>What do you think?
> >>      
> >>
> >I am sorry that I did not publish any code in the original post.  I have
> >run into back/forward slash issues on windows before; and I hoped that
> >there was a simple, code-agnostic solution.
> >
> >I have reduced the Perl code to this:
> >
> >    
> >
> 
> I ran it like this and it seems OK, but I don't have the same conditions:
> 
> use diagnostics;
> use strict;
> use warnings;
> 
> my $prog = "E:/usr/ov/bin/nvhotbackup.bat";
> my $dir = 'E:/backup';
> 
> if (not -d $dir) {
> 	mkdir $dir or die "mkdir $dir: $! ($^E)";
> }
> 
> my $dest = timestamp ();
> if (not -d "$dir/$dest") {
> 	mkdir "$dir/$dest" or die "mkdir $dir/$dest: $! ($^E)";
> }
> 
> do_prog ($prog, $dir, $dest);
> 
> exit 0;
> 
> # Run system command & return exit code
> 
> sub do_prog {
> 	my ($prog, $dir, $dest) = @_;
> 
> $dir =~ s!/!\\!g;	# this one you definitely need
> $prog =~ s!/!\\!g;	# this may be optional
> my $cmd = qq{$prog "$dir" $dest};
> print "CMD == ", $cmd, "\n";
> 
> # my $null = "NUL";
> # system "$cmd >$null 2>&1";
> 
> system $cmd;		# seems OK
> 
> # my @res = `$cmd`;	# also tried this OK
> #print "res='@res'\n";
> 
> }
> 
> # Get date & time string
> 
> sub timestamp {
> 	@_ = localtime ($_[0] ? shift : time);
> return sprintf "%d%02d%02d%02d%02d%02d", $_[5]+1900, $_[4]+1, $_[3], $_[2],
>   $_[1], $_[0];
> 
> }
> 
> __END__
>   
> 
Attachments:
unknown1
unknown2
unknown3
unknown4

Thread:
Michael D Schleif
Michael D Schleif
Chris Wagner
$Bill Luebkert
$Bill Luebkert
Michael D Schleif
$Bill Luebkert
Michael D Schleif
$Bill Luebkert
Michael D Schleif
$Bill Luebkert
James Sluka
Ted Zeng
$Bill Luebkert
Ted Zeng
Paul
Peter Eisengrein
Trevor Joerges
Jim Guion
$Bill Luebkert
James Sluka
$Bill Luebkert
Chris Wagner

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved