Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

System Command mv not working - Require a solution without modules.

by G Nagasri Varma (Novice)
on Jan 28, 2016 at 08:27 UTC ( [id://1153822]=perlquestion: print w/replies, xml ) Need Help??

G Nagasri Varma has asked for the wisdom of the Perl Monks concerning the following question:

1.

HDCS9-156039439P:defunct abc$ ls -ltr total 320 drwxr-xr-x 2 abc staff 68 Jan 28 12:46 1082970465 -rwxrwxrwx 1 abc staff 33 Jan 28 12:47 DEFUNCT.txt drwxr-xr-x 3 abc staff 102 Jan 28 13:20 search_data -rwxrwxrwx 1 abc staff 0 Jan 28 13:29 1087016121 -rw-r--r-- 1 abc staff 155236 Jan 28 13:33 output_defunctusers_move +ment.txt -rwxrwxrwx 1 abc staff 680 Jan 28 13:33 data.pl drwxr-xr-x 2 abc staff 68 Jan 28 13:42 1078036045



2.
HDCS9-156039439P:defunct abc$ cat DEFUNCT.txt 1087016121 1082970465 1078036045
3.

HDCS9-156039439P:defunct abc$ cat data.pl #!/usr/bin/perl open $outputfile, ">>","output_defunctusers_movement.txt" or die "$0: +open : $!"; $count=0; open(DATA, "</Users/abc/defunct/DEFUNCT.txt") or die "Couldn't open fi +le /Users/abc/defunct/DEFUNCT.txt ,$!"; @lines= <DATA>; for ($i=0;$i<=$#lines;$i++) { print $lines[$i]; `mv /Users/abc/defunct/"$lines[$i]" /Users/abc/defunct/search_data`; $count++; print $outputfile "$count . Successfully moved file : $lines[$i] \n"; } close(DATA) || die "Coulnt close file /Users/abc/defunct/DEFUNCT.txt p +roperly";
4.
o/p :
1087016121 mv: rename /Users/abc/defunct/1087016121 to /Users/abc/defunct/search_data/1087016121 : No such file or directory 1082970465 mv: rename /Users/abc/defunct/1082970465 to /Users/abc/defunct/search_data/1082970465 : No such file or directory 1078036045 mv: rename /Users/abc/defunct/1078036045 to /Users/abc/defunct/search_data/1078036045 : No such file or directory HDCS9-156039439P:defunct abc$
Why is the move command not working? I need a solution without modules. Folders do exists but why is it showing an error no such file or directory?

Replies are listed 'Best First'.
Re: System Command mv not working - Require a solution without modules.
by Corion (Patriarch) on Jan 28, 2016 at 08:38 UTC

    The filenames have newlines at the end. If you had used the Perl built-in rename function, it would have told you that your filenames have newlines at the end.

      I tried chomp function "chomp($lines$i);" but it throws me the error Illegal division by zero at ./data.pl line 13, <DATA> line 3.

        Please post the relevant code and please use <code>...</code> tags to mark it so it renders and downloads nicely.

        The problem is mostly a syntax error in your code where Perl interprets a file path as a division of two values. This mostly happens when you omit a closing quote on a string.

Re: System Command mv not working - Require a solution without modules.
by 2teez (Vicar) on Jan 28, 2016 at 08:57 UTC
    Hi G Nagasri Varma,

    There are several things one could point out to you namely:

    • Use <code> </code> around your code
    • use warnings;
      use strict; in your code
    • Your open function should use the 3 arguments option
    • Change your C/C++ for type. Use perl's like so:  foreach () {...}
      OR for () {...}
    • Lastly, instead of using system(..) or backticks `` or qx/../
      use module File::Copy qw/move/ It comes installed with your Perl, for easy portability

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: System Command mv not working - Require a solution without modules.
by Discipulus (Canon) on Jan 28, 2016 at 08:40 UTC
    Hello,

    please add code tags to your post, see How do I post a question effectively?
    probably you need to pass a full path to the command as "c:/users..." and(not noticed the linux directory output..) also note that the name DATA for your filehandle is a poor choice: DATA is __DATA__ (you can reopen it but is an unneeded risk)

    Check your path, for example sourrinding it with square brackets and printing it, before using.

    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: System Command mv not working - Require a solution without modules.
by Anonymous Monk on Jan 28, 2016 at 09:22 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1153822]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (1)
As of 2024-04-25 00:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found