smanicka has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, Was wondering if someone could give me ideas as to how to access a remote server and move files around as per some criteria specified.I figure that i have to use something like file::remote.pm but can't find any good tutorials for that.I'm a beginner.

I would love to hear any other ways of doing this.My perl script cannot reside on the server since its used for multiple projects but the reqmnt is that i need to access move and copy certain files in a dump folder.

Thanks
Smanicka
--------------------------------------------------------------------------
Dec 05 2008
Here is an update on what I have done so far.
This is the code to get the list of files in my directory H:\What_to_do and to look up what to do with the m based on the information specified by the filename.

The file names are given as SYSX%RPT%JOB%TIMEDATEstamp.txt.
The Database is an access database that i am using.It looks like this:

+--------+-------------+---------+------+------+
|Sys_Acc | Report_name |Job_name | Dump |Print |
+--------+-------------+---------+------+------+
|SYSX    |  abc        | 123a    | yes  |  no  |
+--------+-------------+---------+------+------+
|SYSY    |  ade        | 783a    | no   |  yes |
+--------+-------------+---------+------+------+ 

and some same file names are here:
SYSX%abc%123a%12.03.2008.01.32.22.txt
SYSX%abc%123a%12.03.2008.01.32.22.txt
#!usr/bin/perl -w use DBI; use File::Copy; @files=<H:/what_to_do/*>; print "+---------------------------------------------+\n"; print "| Here are the list of files in the directory |\n"; print "+---------------------------------------------+\n\n"; foreach my $file (@files){ my @arr=split(/\//,$file); print "\n"; my @field=split(/%/,$arr[2]); print $arr[2]."\n\n................................................... +.......\n"; my @arr1=split(/\./,$field[3]); pop(@arr1); my $sec=pop(@arr1); my $min=pop(@arr1); my $hr=pop(@arr1); my $date_string=join('-',@arr1); my $sysacc=$field[0]; my $rid=$field[2]; my $jid=$field[1]; my $time=$hr.":".$min.":".$sec; #print "System:\t\t $sysacc \nreport id:\t$rid \njob id:\t\t$jid\ndate + stamp:\t$date_string\ntime :\t\t$hr:$min:$sec \n"; #print "============================================================== +=\n"; my $oldloc=$file; $dbh = DBI->connect('dbi:ODBC:driver=microsoft access driver (*.mdb);d +bq=C:\Documents and Settings\smanicka\Desktop\public_html\lookup_test +.mdb') or die "Sorry,Cant connect\n $DBI::errstr \n"; my $query= "select * from lookup where Sys_Acc=\'$sysacc\' AND Job_nam +e=\'$jid\' AND Report_name=\'$rid\'"; my $sth=$dbh->prepare($query); $sth->execute(); my $status=0; my @stash; while($arr_ref=$sth->fetchrow_array()){ #$status=1; push @stash,[@$array_ref]; } foreach $array_ref(@stash){ print "ROw: @$array_ref \n"; } #if($status==0){ #print " not found"; #} #else{ #print "found";} ## where Sys_Acc=\'$sysacc\'& Job_name=\'$jid\' & Report_name=\'$rid\' #$newloc="h:/newtest/$sysacc/$arr[2]"; #move($oldloc,$newloc)or die "fail to move"; }


Right now I am messing about on my local machine witht he file movement.
The problem is , the $sth->fetchrow_array() doesnt seem to ever return a value. Could anyone clarify this for me? Thank you Monks.
Smanicka
PS. I'm sorry if i am sending this info to anyone's account etc, I m still trying to figure out how to navigate around perlmonk.com and am not sure I am doin things right
  • Comment on need to access a remote server to move the files around in a set folder there
  • Download Code

Replies are listed 'Best First'.
Re: need to access a remote server to move the files around in a set folder there
by kennethk (Abbot) on Dec 04, 2008 at 19:01 UTC
      I know that..but my script can not reside there due to some business rules.That server is used only for dumping and retrieving files.period . :( and yes, its the same topic.sorry about that.I was not a registered user yesterday and I couldn't find my annonymous post

        Is your remote server Unix-like or Windows based?

        Assuming a Unix-like system you are not allowed to run code on, looks like File::Remote would do what you need if your file analysis requires looking at file contents. If you are only interested in file names, mod dates, etc. you should probably use a command line interaction like Net::SSH or Net::Telnet, depending on what the server supports and what runs on your machine. Worst case, Net::FTP can do everything you need, though it would require you to transfer all files over the network twice to move them (not strictly true, but true enough).

        And listen to MidLifeXis - he's smarter than me.

Re: need to access a remote server to move the files around in a set folder there
by MidLifeXis (Monsignor) on Dec 04, 2008 at 18:35 UTC

    What tools do you have available to you? SSH, ftp, NFS, SMB?

    --MidLifeXis

      i know they use ftp around here..if i was not clear, let me explain how things are planned.

      perl script to perform lookup from database as to where to move the files present in the dump folder ( the movement is criteria based - depends on filename) resides on a local machine (unix.. am not sure of which version).

      The server contains a dump folder where all the files of batch processing are dumped by other processes.

      my script should move the files from dump folder on the server to two other folders on the same server, namely print and review.

      I am just getting the hang of making this work on my local machine.as in moving from one folder to another bacsed on what i look up.

      As far as my research goes , i found out that file::remote would work.And I was hoping i could do this using ssh.Could you explain if i need special stuff to do this?
      Thanks

        You may want to start with ssh modules on CPAN and see what you find. Also search for ssh on perlmonks. This is a common question, and search / supersearch can be your friends.

        --MidLifeXis

      I did ask my coworker and she said she's used ftp on that server before..i will clarify if it supports ssh asap.. thanks
Re: need to access a remote server to move the files around in a set folder there
by smanicka (Scribe) on Dec 05, 2008 at 20:00 UTC
    Update: Now if i uncomment the status fields, it prints out foun..hmmm but why not the data in the rows? could anyone help?
Re: need to access a remote server to move the files around in a set folder there
by smanicka (Scribe) on Dec 10, 2008 at 19:11 UTC
    UPDATE
    Works fine so far.
    Now I am in the process of building a CGI program to insert, update and delete from the database.
    here are the problems That I face.
    1. The db is as decribed.There is a auto number column in the table that acts as the primary key.
    2. I am trying to insert just the values for sys, report and job for the table from user input.
    3. I am using the following statement to insert. INSERT INTO LOOKUP (SYS_ACC,REPORT_NAME,JOB_NAME) VALUES \'$sys\',\rpt\',\'$job\'
    4. my $sqlstatement="insert into lookup (Sys_Acc) values \'$sys_acc\' "; print "<br> $sqlstatement"; $sth = $dbh->prepare($sqlstatement) or die "$DBI::errstr"; $sth->execute(); $dbh->commit(); $dbh->disconnect() or warn "Disconnection Failed \t : $DBI::errstr \n" +;
      Even if i try just one simple thing, it gives ora 42000 SQL error ie SYNTAX ERROR.

    Can anyone help me out?Any suggestions ?

      Well, I would say kowtow towards the error message and admit to yourself you may have an error in your SQL syntax. Denial is the first stage! Looks like you are missing parenthesis?

      In general, RTFM if you get into trouble. Type SQL INSERT INTO at Google and click I'm feeling lucky. You are also still not using prepare and execute properly IMO like they are meant to be used (and are described in the manual).

      # For example: my $sql = "insert into lookup (SYS_ACC,REPORT_NAME,JOB_NAME) values (? +,?,?)"; my $sth = $dbh->prepare($sql) or die $dbh->errstr; $sth->execute($sys_acc, $report, $job) or die $sth->errstr #update: fo +rgot error check # Easier to read and how it was intended.

      Or you could use do:

      my $countofrowsaffected = $dbh->do($sql, undef, $sys_acc, $report, $jo +b) or die $dbh->errstr;

      I tend to test SQL statements on their own outside of perl before I put them in code. I use mysql's command line client but there are many alternatives.