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 Michael D Schleif other posts by this author
Oct 28 2005 5:35AM messages near this date
view in the new Beta List Site
Back-slashes & calling a batch file from perl ??? | Re: Back-slashes & calling a batch file from perl ???
* 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:

    #! /usr/bin/perl

    require 5;
    use diagnostics;
    use strict;
    use warnings;

    my $prog = "E:/usr/ov/bin/nvhotbackup.bat";
    my $dir = 'E:/backup';

    my $dest = timestamp();
    mkdir "$dir/$dest";
    do_prog($prog, $dir, $dest);

    exit 0;

    # Run system command & return exit code
    sub do_prog {
        my ($prog, $dir, $dest) = @_;

    # $dir =~ s!/!\\\\!g;

        my $cmd = join " ", $prog, $dir, $dest;
        print "CMD == ", $cmd, "\n";

    # return 1;

        my $null = "NUL";
    #   system "$cmd > $null 2>&1";
        system "$cmd";
        1;
    }

    # 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];
    }

==================================================


[A] As is, $prog fails like this:

      Invalid switch - "backup\20051028070933".

    Nevertheless, $dir/$dest *DOES* get created.

[B] When I do any of these in do_prog:

      $dir =~ s!/!\\!g;
      $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?


-- 
Best Regards,

mds
mds resource
877.596.8237
-
Dare to fix things before they break . . .
-
Our capacity for understanding is inversely proportional to how much
we think we know.  The more I know, the more I know I don't know . . .
--
Attachments:
signature.asc
unknown1
unknown2
unknown3

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