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 Chris Wagner other posts by this author
Oct 29 2005 8:52AM 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 ???
Forgive me for being so late to jump in.  I'm sure my withdrawn rationality
will prevail.  We need to get back to basic debugging procedures.  Verify
that something does what u think it does.

Mkdir:
D:\> perl
mkdir "a/b/c/d";
-d "/a/b/c/d" ? print "dirs exist" : print "dirs don't exist";
^D
dirs don't exist

Your mkdir fails because Perl cannot recursively create directories.
Atleast on build 813.  In ur two dir example it would work if dir1 already
existed. Maybe an interbuild change?  Use shell builtin:
D:\> perl
system "cmd /c mkdir a\\b\\c\\d";
-d "a/b/c/d" ? print "dirs exist" : print "dirs don't exist";
^D
dirs exist


At 07:30 AM 10/28/2005 -0500, Michael D Schleif wrote:
> [A] As is, $prog fails like this:
> 
>       Invalid switch - "backup\20051028070933".
> 
>     Nevertheless, $dir/$dest *DOES* get created.

It says invalid switch because the full arg as passed by perl is
E:/backup\20051028070933.  Everything after the / is interpreted as a switch
by the Windows program - not the batch file.  Simply flip that ONE / into a
\ immediately before the system call.


>     mkdir "$dir/$dest"
>         or die "\n\n\tERROR: Cannot create \'$dir/$dest\' : $! : $?\n\n";
> 
>     Nor, does it get created ;<

Don't have an answer for that one.


This code works for me.  Copy and paste this and see what happens.  Dir1
should not preexist.  What args does the bat file see?  Also notice the
system command.  It has weird quotishness that I think has been overlooked.
It does *not* reevaluate \ escapes in whatever was passed to it.  However,
if you feed it a quoted string, the quotes "operator" *will* evaluate escapes before passing
 that string to system.

===test.pl===
    my $prog = "D:/testbat.bat";
    my $dir = 'D:/dir1';

    my $dest = timestamp();
    mkdir $dir;
    mkdir "$dir/$dest";
    -d "$dir/$dest" ? print "dirs exist\n" : print "dirs don't exist\n";
    do_prog($prog, $dir, $dest);

    sub do_prog {
        my ($prog, $dir, $dest) = @_;
        $dir =~ s!/!\\!g;
        my $cmd = join " ", $prog, $dir, $dest;
        print "CMD is ", $cmd, "\n";
        system $cmd;
    }

    sub timestamp {
        @_ = localtime($_[0] ? shift : time() );
        return sprintf "%d%02d%02d%02d%02d%02d", $_[5] + 1900,$_[4] +
1,$_[3],$_[2],$_[1],$_[0];
    }

======

===testbat.bat===
@echo off
echo testbat.bat
echo Arg 1 is %1
echo Arg 2 is %2
echo Arg 3 is %3

======


Here is my output:
D:\> perl test.pl
dirs exist
CMD is D:/testbat.bat D:\dir1 20051029113850
testbat.bat
Arg 1 is D:\dir1
Arg 2 is 20051029113850
Arg 3 is










--
REMEMBER THE WORLD TRADE CENTER         ---=< WTC 911 > =--
"...ne cede malis"

00000100

_______________________________________________
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