Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question PERL Script Won't Copy Folders, Only Files

    This is part of a much larger script, it's supposed to copy both files and folders, but it only copies files. Rather than post the entire thing here, I'm posting the part that I *think* has the problem.

    Anyone know why this won't copy a folder? Or do I have to post the whole thing?


    $cp_file_path = "$core_data_path/" . $cp_dir . $cp_file;

    $new_file_path = "$core_data_path/$new_full_name";

    if (-e $new_file_path) {
    $copied .= "<font color='#550000'>File</font> <font color='wheat'>$cp_file</font> <font color='#550000'>already exists in</font> <a href=\"$FM?dirname=$new_dir\"><font color='silver'>$new_dir/</font></a>.<br /><br />";
    }

    else { `cp -R $cp_file_path $new_file_path`;
    if (-e $new_file_path)

  • #2
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    I guess the backticks in your else clause are causing some difficulties. I don't use them.

    check that your if/else condition is working as you think it is and then perhaps try this code.

    Code:
     system ("cp -r /var/www/vhosts/example.com/httpdocs/FILES/$existing_file  /var/www/vhosts/example.com/FILES/$new_location")  || "cant copy across";
    That's not a good error message in my snippet but the code is working.

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #3
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,944
    Thanks
    2
    Thanked 170 Times in 165 Posts
    Don't use backticks in void context.

    Instead of shelling out to cp, use the Perl module designed for this purpose.

    http://search.cpan.org/~dmuey/File-C...8/Recursive.pm

  • #4
    New Coder
    Join Date
    Oct 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I dropped the backticks, but it still doesn't work. This is the whole original code, as-is.
    It does copy files, but for some reason it won't copy folders.

    Code:
    # Begin Copy Files and Directories
    sub confirm_copy {
    # Text output 
    $sel_files = ''; $sel_dirs = '';
    if (@file_names) {$sel_files = "files";}
    if (@dir_names) {$sel_dirs = "directories";}
    if (@file_names && @dir_names) {$sel_both = " and ";}
    if ($dirname eq '') {$stay_dir = "Main Directory";}
    else {$stay_dir = $dirname;}
    
    if ($sel_files || $sel_dirs) {
    print qq~<script language=javascript type=text/javascript>
    function selnewdir(newdir) {
    document.copy.new_dir.value = newdir;
    if (document.copy.stay.checked != true) {document.copy.dirname.value = newdir;}}
    function stayindir() 
    {if (document.copy.stay.checked != true) 
    {document.copy.dirname.value = document.copy.new_dir.value;}
    else {chdir ('$working_dir$dirname');}}
    </script>
    
    <p><font size="5" face="verdana"><b>Copy These <font size="5" color="wheat" face="verdana">$sel_files$sel_both$sel_dirs</font> From:</b></font> <font size="5" color="silver" face="verdana"><b>$stay_dir</b></font><br />
    <table align="center" width="100%" cellspacing="0" cellpadding="4" border="0">
    <tr><td align="right" valign="top"><font size="5" face="verdana"><b>To:</b></font></td>
    <td><form method="POST" action="$cgi_url" name="copy"><input type="text" size="99" name="new_dir" value="$dirname" onChange="selnewdir(this.value)" class="text"></td></tr>
    <tr><td>&nbsp;</td><td><select style="width:200px; font-weight:normal; font-size:12pt;" onChange="selnewdir(this.value)">
    <option value="">Main~; &DirList;
    print qq~</select></td></tr></table></p>
    
    <center><font size="5"><b>Enter New Names In The Boxes, Or Just Leave Them As They Are</b></font></center><br />
    <table align="center" cellspacing="0" cellpadding="3" border="0">~;
    foreach $cp_dir (@dir_names) 
    {print qq~<tr><td align="left"><font size="5" color="silver"><b>* </b></font><font size="5" color="silver"><b>$cp_dir</b></font><input type="hidden" name="dir_names" value="$cp_dir"></td>
    <td><input type="text" size="43" value="$cp_dir" name="$cp_dir" class="text" onMouseover="focus()"></td></tr>~;}
    foreach $cp_file (@file_names) 
    {print qq~<tr><td align="left"><font size="5" color="wheat"><b>* </b></font><font size="5" color="wheat"><b>$cp_file</b></font><input type="hidden" name="file_names" value="$cp_file"></td>
    <td><input type="text" size="43" value="$cp_file" name="$cp_file" class="text" onMouseover="focus()"></td></tr>~;}
    print qq~</table><p>
    <input type="hidden" name="last_dir" value="$dirname">
    <input type="hidden" name="dirname" value="$dirname">
    <input type="checkbox" name="stay" value="yes" onClick="stayindir()" checked>&nbsp;<font size="5" face="verdana"><b>Stay in <font size="5" color="silver">$stay_dir</font> after Copy?</b></font>
    
    <p>
    
    <table cellspacing="0" cellpadding="3" border="0">
    <tr><td><input type="submit" name="action" value="Confirm Copy" style="width:145px; font-weight:bold; font-size:12pt; cursor:pointer;" class="buts"></b></form></td></tr>
    <tr><td align="center"><form method="POST" action="$cgi">
    <input type="hidden" name="action" value="abort">
    <input type="submit" value="Cancel" style="width:145px; font-weight:bold; font-size:12pt; cursor:pointer;" class="butc"></form></td></tr></table>~;}
    else {&report('You must choose one or more files to use Copy.'); &list_files;}}
    
    sub copy { 
    $last_dir = $FORM{'last_dir'};
    $new_dir = $FORM{'new_dir'};
    $copied = &checkname($new_dir);
    if ($last_dir eq $dirname) { $show_last_dir = "";}
    elsif (!$last_dir && $dirname eq $new_dir) {$show_last_dir = "Main/";}
    elsif ($dirname eq $new_dir) {$show_last_dir = "$last_dir/"; }
    if (!$new_dir) {$show_new_dir = "Main/";}
    if ($last_dir) {$cp_dir = "$last_dir/";}
    if ($new_dir) {$new_dir_path = "$core_data_path/$new_dir";}
    else { $new_dir_path = $core_data_path;}
    if (!-e $new_dir_path) {
    mkdir $new_dir_path;
    
    `chmod 755 $new_dir`; If I drop the backticks, this line kills the script.
    
    if (-e $new_dir_path) {$copied .= "<font color=#550000>Directory</font> <a href=\"$FM?dirname=$new_dir\"><font color=silver>$new_dir/</font></a> <font color=#550000>was created</font>.<br />";} 
    else { $copied .= "<font color=#550000>Directory</font> <font color=silver>$new_dir</font> <font color=#550000>was NOT created</font>.<br />";}}
    foreach $cp_file (@file_names) 
    {$new_file = $FORM{$cp_file};
    
    # Create new file path from choices 
    if ($new_dir && $new_file) {$new_full_name = "$new_dir/$new_file";}
    elsif ($new_dir && !$new_file) {$new_full_name = "$new_dir/$cp_file";}
    elsif (!$new_dir && $new_file) {$new_full_name = $new_file;} 
    # Main, new name 
    else {$new_full_name = $cp_file;} 
    # Main, same name
    $copied .= &checkname($new_full_name);
    
    # Display file path for the directory that was not listed 
    if ($dirname eq $last_dir) { # Stayed in the same directory
    $cp_file_name = $cp_file;
    if ($dirname eq $new_dir) { # Copied to current directory 
    if ($new_file) { $new_file_name = $new_file;}
    else { $new_file_name = $cp_file;}}
    else { # Copied file to different directory 
    $new_file_name = $show_new_dir . $new_full_name;}}
    else { # Changed to a different directory 
    $cp_file_name = $show_last_dir . $cp_file;
    if ($dirname eq $new_dir) 
    { # Copied file to current directory 
    if ($new_file) {$new_file_name = $new_file;}
    else {$new_file_name = $cp_file;}}
    else {$new_file_name = $new_full_name;} 
    # Copied file to different directory
    }
    
    $cp_file_path = "$core_data_path/" . $cp_dir . $cp_file;
    $new_file_path = "$core_data_path/$new_full_name";
    if (-e $new_file_path) {$copied .= "<font color='#550000'>File</font> <font color='wheat'>$cp_file</font> <font color='#550000'>already exists in</font> <a href=\"$FM?dirname=$new_dir\"><font color='silver'>$new_dir/</font></a>.<br /><br />";}
    else { `cp $cp_file_path $new_file_path`;
    if (-e $new_file_path) 
    {$copied .= "<font color='#550000'>File</font> <font color='wheat'>$cp_file_name</font> <font color='#550000'>was copied to</font> <a href=\"$FM?dirname=$new_dir\"><font color='silver'>$new_dir/</font></a>.<br /><br />";} 
    else {$copied .= "<font color='#550000'>File</font> <font color='silver'>$cp_file_name</font> <font color='#550000'>was NOT copied to</font> <a href=\"$FM?dirname=$new_dir\"><font color='silver'>$new_dir/</font></a>.<br /><br />";}}}
    &report($copied);} 
    # End Copy Files and Directories
    Last edited by Phred; 01-13-2011 at 05:58 AM.

  • #5
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    OK but, did you try what FishMonger suggested? Oh yeh - don;t forget to put your code in [ co de] tags (without spacing). Much easier to follow.
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #6
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,944
    Thanks
    2
    Thanked 170 Times in 165 Posts
    Wow, that code is almost impossible to follow.

    Please fix your indentation and repost before we continue And, use the standard code tags instead of the icode tags. If you need to emphasis a section, you can add color and/or other attributes to make those sections stand out.

    I dropped the backticks, but it still doesn't work.
    No you didn't...not in the code you posted. You're still issuing the cp, mkdir and chmod commands via backticks in void context. None of that is needed because Perl has built-in mkdir and chmod commands and the File::Copy and File::Copy::Recursive modules for dealing with the copy step.

    Based on the code you posted, it appears that you're not using the strict pragma and you're probably not using the warnings pragma either. Leaving those out is a very big mistake.

  • #7
    New Coder
    Join Date
    Oct 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Okay, I changed this line `chmod 755 $new_dir`;

    to the line below (as you can see, it wasn't written correctly)

    chmod(755, "$new_dir");

    Also, this is the first line:


    #!/usr/local/bin/perl
    use warnings;
    require 'FMRoutines.lib';
    &Settings;


    I will look into using that module, but I'm not sure how to call the function.
    Last edited by Phred; 01-13-2011 at 07:06 AM.

  • #8
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,944
    Thanks
    2
    Thanked 170 Times in 165 Posts
    That should be:
    Code:
    chmod(0755, $new_dir);
    However, if you're using Perl's methods for creating the directory, the mode could and should be specified in the call to create the directory instead of using a separate call to chmod.

    For the file copy portion, the module I pointed to is one option and that documentation provides examples. Another module choice would be File::Copy http://search.cpan.org/~jesse/perl-5...b/File/Copy.pm and for creating the directory path, especially if you need to create intermediary directories, I'd use File::Path http://search.cpan.org/~dland/File-Path-2.08/Path.pm
    Last edited by FishMonger; 01-13-2011 at 02:59 PM.

  • #9
    New Coder
    Join Date
    Oct 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I'm not familiar with installing PERL modules, so this is what I have at the top:


    #!/usr/local/bin/perl
    use warnings;
    use File::Copy::Recursive qw(fcopy rcopy dircopy fmove rmove dirmove);


    Is this correct?

    Once I get this part correct, I will start on how to call the modules from within the script...

  • #10
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,944
    Thanks
    2
    Thanked 170 Times in 165 Posts

  • #11
    New Coder
    Join Date
    Oct 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanx!

    btw, I'm running this program on a domain - not on my PC. It's part of a home-made file manager.

    Okay, got the modules installed (had to ask my hosting company), now I have to make them work.
    Last edited by Phred; 01-17-2011 at 01:03 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •