469,362 Members | 2,548 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

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??
Jul 31 '07 #1
13 7074
numberwhun
3,503 Expert Mod 2GB
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::Recursive.

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:

Expand|Select|Wrap|Line Numbers
  1.    `ps -ef | grep <process>`
  2.  
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
Jul 31 '07 #2
KevinADC
4,059 Expert 2GB
if on windows:

c:\>xcopy /?



Expand|Select|Wrap|Line Numbers
  1. Copies files and directory trees.
  2.  
  3. XCOPY source [destination] [/A | /M] [/D[:date]] [/P] [/S [/E]] [/W]
  4.                            [/C] [/i] [/Q] [/F] [/L] [/H] [/R] [/T] [/u]
  5.                            [/K] [/N]
  6.  
  7.  source       Specifies the file(s) to copy.
  8.  destination  Specifies the location and/or name of new files.
  9.  /A           Copies files with the archive attribute set,
  10.               doesn't change the attribute.
  11.  /M           Copies files with the archive attribute set,
  12.               turns off the archive attribute.
  13.  /D:date      Copies files changed on or after the specified date.
  14.               If no date is given, copies only those files whose
  15.               source time is newer than the destination time.
  16.  /P           Prompts you before creating each destination file.
  17.  /S           Copies directories and subdirectories except empty ones.
  18.  /E           Copies directories and subdirectories, including empty ones.
  19.               Same as /S /E. May be used to modify /T.
  20.  /W           Prompts you to press a key before copying.
  21.  /C           Continues copying even if errors occur.
  22.  /I           If destination does not exist and copying more than one file,
  23.               assumes that destination must be a directory.
  24.  /Q           Does not display file names while copying.
  25.  /F           Displays full source and destination file names while copying.
  26.  /L           Displays files that would be copied.
  27.  /H           Copies hidden and system files also.
  28.  /R           Overwrites read-only files.
  29.  /T           Creates directory structure, but does not copy files. Does not
  30.               include empty directories or subdirectories. /T /E includes
  31.               empty directories and subdirectories.
  32.  /U           Updates the files that already exist in destination.
  33.  /K           Copies attributes. Normal Xcopy will reset read-only attributes.
  34.  /Y           Overwrites existing files without prompting.
  35.  /-Y          Prompts you before overwriting existing files.
  36.  /N           Copy using the generated short names.
may vary from one version of windows to another
Jul 31 '07 #3
Hi,
following piece of code i tried,and used file::copy module which u had suggested

Expand|Select|Wrap|Line Numbers
  1. Fn_Copy("D:\\SticUtils", "$path",
  2.     qw(SticConf.exe,sticpref.exe,sticdump.exe,sticclient.exe)
  3. );
  4.  
  5. sub Fn_Copy {
  6.     my $Sourc = $_[0];
  7.     my $Destination = $_[1];
  8.     @_ = ($_[2],$_[3],$_[4],$_[5]);
  9.  
  10.     foreach ( @_) {
  11.         print $Sourc."\\".$_;
  12.         print "\n";
  13.         print $Destination."\\".$_;
  14.         copy($Sourc."\\".$_, $Destination."\\".$_);
  15.     }
  16. }
  17.  
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??
Aug 1 '07 #4
KevinADC
4,059 Expert 2GB
do you see what the problem is by doing this:

Expand|Select|Wrap|Line Numbers
  1. Fn_Copy ("D:\\SticUtils", "$path", qw(SticConf.exe,sticpref.exe,sticdump.exe,sticclient.exe));
  2.  
  3.  
  4. sub Fn_Copy {
  5.    my $Sourc = $_[0];
  6.    my $Destination = $_[1];
  7. #   @_ = ($_[2],$_[3],$_[4],$_[5]);
  8.    print $_[2];
  9. }
look at what gets printed, it's not "SticConf.exe".
Aug 1 '07 #5
true it prints all the data as $_[2]..any idea how can i fix it!!
Aug 1 '07 #6
KevinADC
4,059 Expert 2GB
one way to send and recieve arguments to a function:

Expand|Select|Wrap|Line Numbers
  1. Fn_Copy ('D:/SticUtils', $path, 'SticConf.exe', 'sticpref.exe','sticdump.exe', 'sticclie nt.exe');
  2.  
  3.  
  4. sub Fn_Copy {
  5.    my $Sourc = $_[0];
  6.    my $Destination = $_[1];
  7.    my @Files = @_[2..5];
  8.    foreach my $files (@Files) {
  9.       do something with $files
  10.    }
  11. }
the above is a "non destructive" way: the contents of @_ are not changed.

A "destructive" way is:

Expand|Select|Wrap|Line Numbers
  1. Fn_Copy ('D:/SticUtils', $path, 'SticConf.exe', 'sticpref.exe','sticdump.exe', 'sticclie nt.exe');
  2.  
  3.  
  4. sub Fn_Copy {
  5.    my $Sourc = shift;
  6.    my $Destination = shift;
  7.    my @Files = @_;
  8.    foreach my $files (@Files) {
  9.       do something with $files
  10.    }
  11. }
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
Aug 1 '07 #7
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.
Aug 1 '07 #8
KevinADC
4,059 Expert 2GB
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\perl.exe scriptname.pl

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

perl scriptname.pl
Aug 1 '07 #9
you answered my question other way, i want to invoke command prompt using a perl script and then execute some commands on it.
Aug 1 '07 #10
KevinADC
4,059 Expert 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. print "Enter some data:"
  2. chomp(my $input = <STDIN>);
  3. print $input;
  4.  
Is this what you mean?
Aug 1 '07 #11
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.
Aug 1 '07 #12
KevinADC
4,059 Expert 2GB
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:

http://perldoc.perl.org/

another helpful link:

http://www.perl.org/books/beginning-perl/

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
Aug 1 '07 #13
numberwhun
3,503 Expert Mod 2GB
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
Aug 1 '07 #14

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

10 posts views Thread by Alex Vinokur | last post: by
3 posts views Thread by Peter van der Veen | last post: by
8 posts views Thread by utab | last post: by
reply views Thread by mlfblom | last post: by
6 posts views Thread by kimiraikkonen | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.