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 $Bill Luebkert other posts by this author
Oct 28 2005 7:26AM messages near this date
view in the new Beta List Site
Re: Back-slashes & calling a batch file from perl ??? | Re: Back-slashes & calling a batch file from perl ???
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__

>  
>  [A] As is, $prog fails like this:
>  
>        Invalid switch - "backup\20051028070933".
>  
>      Nevertheless, $dir/$dest *DOES* get created.

That's because you do a mkdir in the script ??

>  [B] When I do any of these in do_prog:
>  
>        $dir =~ s!/!\\!g;

This should be fine for any args to the bat file.

>        $dir =~ s!/!\\\\!g;
>        $dir =~ s!/!\\\\\\!g;
>        $dir =~ s!/!\\\\\\\\!g;
>  
>      I do *NOT* get errors from $prog; but, $dir/$dest does *NOT* get
>      created.  In the real script, I am doing error checking; and the
>      following does *NOT* die:
>  
>      mkdir "$dir/$dest"
>          or die "\n\n\tERROR: Cannot create \'$dir/$dest\' : $! : $?\n\n";
>  
>      Nor, does it get created ;<
>  
>      Without that directory, $prog *CANNOT* do what it is intended to do
>      (e.g., copy files into that directory.)
>  
>  [C] Obviously, when I use the `return 1;', and bypass system(), then the
>      directory gets created, regardless of back or forward slashes.
>  
>  [D] This is supposed to be run as Scheduled Task/cron; so, the $null
>      issue is to eliminate unnecessary noise.  Whether or not I use that
>      in this test code does *NOT* seem to affect the results.
>  
>  [E] $prog itself is copyrighted.  If necessary, I will try and reduce
>      that, and publish it as well.  It is doing basic batch file stuff,
>      setting variables and copying files.  Normally, I would convert its
>      code, and incorporate that into Perl; but, my client is concerned
>      about future upgrades of the large program, whence $prog comes,
>      breaking functionality -- the old customizations broken by other
>      software upgrades problem ;>  My Perl program is to be a wrapper to
>      allow automated, unattended use of this proprietary program.
>  
>  
>  What do you think?
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
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