copying files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tovenkatesh82
    New Member
    • Mar 2007
    • 30

    copying files

    Hi All,
    I want to copy some files from one dir in d:, to another dir in c:, i tried using xcopy but it says too many parameters!!pls give suggestions

    another thing i wanted to know is how can i invoke command prompt from perl??
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Ok, first, when you post a question, it is always recommended that you post the code that you have tried thus far so we can help you tweak it and learn. That said, what have you tried?

    I am not versed in xcopy, but you could always check out the module called File::Copy, which will enable you to copy files. If it is directories that you are looking to move and not just files, then you will want to read up on File::Copy::Rec ursive.

    For anything you want to do, just search CPAN and see what is available.

    As for your "invoking the command line" question, I am not exactly sure what you are asking. If it is as simple as wanting to run a system level command from a Perl script, then just enclose the command and its options in back ticks:

    Code:
       `ps -ef | grep <process>`
    If you are refering to obtaining user input, then you will want to do some reading of the perldoc perlsyn page as it contains examples of the use of the <STDIN> filehandle.

    If that is not what you are looking for, then you will need to please rephrase your question and let us know exactly what you are looking for and looking to do.

    Regards,

    Jeff Kirkland

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      if on windows:

      c:\>xcopy /?



      Code:
      Copies files and directory trees.
      
      XCOPY source [destination] [/A | /M] [/D[:date]] [/P] [/S [/E]] [/W]
                                 [/C] [/I] [/Q] [/F] [/L] [/H] [/R] [/T] [/U]
                                 [/K] [/N]
      
       source       Specifies the file(s) to copy.
       destination  Specifies the location and/or name of new files.
       /A           Copies files with the archive attribute set,
                    doesn't change the attribute.
       /M           Copies files with the archive attribute set,
                    turns off the archive attribute.
       /D:date      Copies files changed on or after the specified date.
                    If no date is given, copies only those files whose
                    source time is newer than the destination time.
       /P           Prompts you before creating each destination file.
       /S           Copies directories and subdirectories except empty ones.
       /E           Copies directories and subdirectories, including empty ones.
                    Same as /S /E. May be used to modify /T.
       /W           Prompts you to press a key before copying.
       /C           Continues copying even if errors occur.
       /I           If destination does not exist and copying more than one file,
                    assumes that destination must be a directory.
       /Q           Does not display file names while copying.
       /F           Displays full source and destination file names while copying.
       /L           Displays files that would be copied.
       /H           Copies hidden and system files also.
       /R           Overwrites read-only files.
       /T           Creates directory structure, but does not copy files. Does not
                    include empty directories or subdirectories. /T /E includes
                    empty directories and subdirectories.
       /U           Updates the files that already exist in destination.
       /K           Copies attributes. Normal Xcopy will reset read-only attributes.
       /Y           Overwrites existing files without prompting.
       /-Y          Prompts you before overwriting existing files.
       /N           Copy using the generated short names.
      may vary from one version of windows to another

      Comment

      • tovenkatesh82
        New Member
        • Mar 2007
        • 30

        #4
        Hi,
        following piece of code i tried,and used file::copy module which u had suggested

        [CODE=perl]
        Fn_Copy("D:\\St icUtils", "$path",
        qw(SticConf.exe ,sticpref.exe,s ticdump.exe,sti cclient.exe)
        );

        sub Fn_Copy {
        my $Sourc = $_[0];
        my $Destination = $_[1];
        @_ = ($_[2],$_[3],$_[4],$_[5]);

        foreach ( @_) {
        print $Sourc."\\".$_;
        print "\n";
        print $Destination."\ \".$_;
        copy($Sourc."\\ ".$_, $Destination."\ \".$_);
        }
        }
        [/CODE]

        the code works fine if there is only one element in array...if i add more than one element it doesnot works.

        i want to invoke command prompt on windows and execute some task on the same, how can i invoke the command prompt using perl script??
        Last edited by miller; Aug 1 '07, 04:03 PM. Reason: Code Tag and ReFormatting

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          do you see what the problem is by doing this:

          [CODE=perl]Fn_Copy ("D:\\SticUtils ", "$path", qw(SticConf.exe ,sticpref.exe,s ticdump.exe,sti cclient.exe));


          sub Fn_Copy {
          my $Sourc = $_[0];
          my $Destination = $_[1];
          # @_ = ($_[2],$_[3],$_[4],$_[5]);
          print $_[2];
          }[/CODE]

          look at what gets printed, it's not "SticConf.e xe".

          Comment

          • tovenkatesh82
            New Member
            • Mar 2007
            • 30

            #6
            true it prints all the data as $_[2]..any idea how can i fix it!!

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              one way to send and recieve arguments to a function:

              Code:
              Fn_Copy ('D:/SticUtils', $path, 'SticConf.exe', 'sticpref.exe','sticdump.exe', 'sticclie nt.exe');
              
              
              sub Fn_Copy {
                 my $Sourc = $_[0];
                 my $Destination = $_[1];
                 my @Files = @_[2..5];
                 foreach my $files (@Files) {
                    do something with $files
                 }
              }
              the above is a "non destructive" way: the contents of @_ are not changed.

              A "destructiv e" way is:

              Code:
              Fn_Copy ('D:/SticUtils', $path, 'SticConf.exe', 'sticpref.exe','sticdump.exe', 'sticclie nt.exe');
              
              
              sub Fn_Copy {
                 my $Sourc = shift;
                 my $Destination = shift;
                 my @Files = @_;
                 foreach my $files (@Files) {
                    do something with $files
                 }
              }
              the contents of @_ are changed but it really does not matter in this context.

              Note: you can use forward slashes with windows in directory paths. You should also almost never put double-quotes around a single scalar variable like you did with $path. It is inefficient and can hide problems that even "strict" and "warnings" may not be able to help you find and fix. Use double-quotes to construct strings that need variable or meta character interpolation and single-quotes to construct strings that do not need variable or meta character interpolation.

              $bar = 'bar'; # good
              $foo = 'foo'; # good
              $foo = "foo"; # bad but works
              $foo = "foo\n"; # good because \n is a meta character
              $foo = "$bar"; # bad but works
              $foo = $bar; # good
              $foo ="$bar $blah"; # good because you had to make a new string
              $foo = $bar . $blah; # the same as above using concatenation

              Comment

              • tovenkatesh82
                New Member
                • Mar 2007
                • 30

                #8
                thanks very much it works fine now.

                Do you have any idea how could i access command prompt using perl script,do i need to use any module?

                I want to execute some commands from command prompt.

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  to run a perl program from the command prompt, you can often do it like this:

                  c:\>perl scriptname.pl

                  if perl is not in the command path you have to use the fully qualified path to perl:

                  c:\>perl\bin\pe rl.exe scriptname.pl

                  or change directory to the c:\perl\bin folder first then type:

                  perl scriptname.pl

                  Comment

                  • tovenkatesh82
                    New Member
                    • Mar 2007
                    • 30

                    #10
                    you answered my question other way, i want to invoke command prompt using a perl script and then execute some commands on it.

                    Comment

                    • KevinADC
                      Recognized Expert Specialist
                      • Jan 2007
                      • 4092

                      #11
                      Originally posted by tovenkatesh82
                      you answered my question other way, i want to invoke command prompt using a perl script and then execute some commands on it.
                      You don't really invoke the command prompt from perl. If you want to run a perl program as a terminal script you start it the way I showed you and prompt for user input. A simple example:

                      Code:
                      print "Enter some data:"
                      chomp(my $input = <STDIN>);
                      print $input;
                      Is this what you mean?

                      Comment

                      • tovenkatesh82
                        New Member
                        • Mar 2007
                        • 30

                        #12
                        kevin:
                        let me give you an example program

                        You have been asked to write a program which ask you to run some exe files from the command prompt.

                        I belive following steps should be followed:
                        1)Invoke the command prompt.
                        2)move to the dir where you have kept your exe file.
                        3)execute the exe.

                        I hope you understand what exactly i am looking at.

                        Comment

                        • KevinADC
                          Recognized Expert Specialist
                          • Jan 2007
                          • 4092

                          #13
                          Originally posted by tovenkatesh82
                          kevin:
                          let me give you an example program

                          You have been asked to write a program which ask you to run some exe files from the command prompt.

                          I belive following steps should be followed:
                          1)Invoke the command prompt.
                          2)move to the dir where you have kept your exe file.
                          3)execute the exe.

                          I hope you understand what exactly i am looking at.
                          I understand, you do not understand. Either you invoke the command prompt yourself, not your perl script, and enter:

                          c:\>perl yourscript.pl[list of arguments]

                          or you run your sript as a terminal based script that prompts for user input. As far as running external applications (exe files for example) you can use;

                          system()
                          qx// (same as backtiks)
                          exec() (but this exits your current perl program)

                          You can look up all the above on the perldoc website:



                          another helpful link:



                          This is as much help as I can give you at this point. If you write some code I can help you with your code.

                          -Kevin

                          Comment

                          • numberwhun
                            Recognized Expert Moderator Specialist
                            • May 2007
                            • 3467

                            #14
                            Originally posted by tovenkatesh82
                            kevin:
                            let me give you an example program

                            You have been asked to write a program which ask you to run some exe files from the command prompt.

                            I belive following steps should be followed:
                            1)Invoke the command prompt.
                            2)move to the dir where you have kept your exe file.
                            3)execute the exe.

                            I hope you understand what exactly i am looking at.
                            I completely agree with Kevin that you don't understand. Step #1 should be removed from your list. You do not invoke a command prompt from a Perl script. Like he said, you can run programs from within your Perl script, either by using system() or enclosing the command call in back tics. That said, you can simply remove step #2 from your list as well because you can simply call your file using its full path.

                            Also, why does this sound distinctly like a homework assignment? I highly recommend "Learning Perl, 4th Edition " for your reading pleasure.

                            Regards,

                            Jeff

                            Comment

                            Working...