Windows Perl Scripting Forums » General

Test if file can be created

(1 post)
  • Started 5 years ago by

No tags yet.


  1. Anonymous

    Need to test if a file can be created before creating it. Sounds simple but isn't. Just checking if the parent folder is writable does not work. Right now I'm doing ...

    my $Path = "$Folder/$File";
    my $CREATE_IT = 0;
    if ( -w "$Path" ) {
    print " File Writable: $Path\n";
    } elsif( -e "$Path" ) {
    print " File Exists but not writable: $Path\n";
    } elsif( -w "$Folder" ) {
    print " File does not exist but ...\n Folder Writable: $Folder\n";
    $CREATE_IT = 1; # Try creating it below
    } elsif( -e "$Folder" ) {
    print " File does not exist but ...\n Folder Exists but not writable: $Folder\n";
    $CREATE_IT = 1; # Try creating it anyway
    } else {
    print " File and Folder does not exist: $Folder\n";
    }
    if ( $CREATE_IT ) {
    open (TSTPT, ">$Folder/$File") or print "+++ Open failed\n";
    print TSTPT " Hello World";
    close TSTPT or print "+++ Close failed\n";
    ! -e "$Folder/$File" ? print "++ File Not Created\n" : print "++ File Created\n";
    }

    When I run this on Win 7 and the file does not exist and Perl claims the parent folder is not writable, the file is successfully created anyway. Yet another folder Perl claims is writable fails when creating the file. Here are my test cases:

    File does not exist but ...
    Folder Exists but not writable: C:/users/userid/documents
    ++ File Created
    File does not exist but ...
    Folder Writable: C:/users/userid/my documents
    ++ File Not Created

    Posted 5 years ago #

RSS feed for this topic

Reply

You must log in to post.