Application Engine PeopleCode to Copy All or Some Files in a Folder to FTP or Database Server

Application Engine PeopleCode to Copy All or Some Files in a Folder to FTP or Database Server

Local File &MYFILE, &LogFile;
Local array of string &FNAMES;
Local number &file_no;
Local string &ATTACHUSERFILE, &ATTACHSYSFILENAME, &fname, &Date_Time;
Local Record &DOC_REC;
Local SQL &SQL1;

&LogFile = GetFile(“\\sy-hr9upg\hrdev\ps\test1\q_fileupload_log.log”, “w”, “a”, %FilePath_Absolute);

/* Set Log File */
&FNAMES = FindFiles(“\\sy-hr9upg\hrdev\ps\test1\*.docx”, %FilePath_Absolute);

/* Store paths of all files(pdf) of a folder to an Array */
&LogFile.WriteLine(“No. of files found: “ | &FNAMES.Len);
&file_no = 1;

&URL_ID = “RECORD://HRS_ATTACHMENTS”;
/*&URL_ID = “ftp://user:password@ftpserver.peoplesoft.com:6000/”;*/
/*&URL_ID = “ftp://user:password@ftpserver.peoplesoft.com:6000/PATH”;*/
/*&URL_ID = “URL.HRS_INT_ATCH”;*/
/* The following goes to the default folder (default folder assigned to the login user id) on the FTP server – example: /sw/app/psoft/psofthr*/
/*&URL_ID = “ftp://pmilind:password@mycompany.com”;*/
/* If the default folder on FTP server is /sw/app/psoft/psofthr then the following puts it to /sw/app/psoft/psofthr/log */
/*&URL_ID = “ftp://pmilind:password@mycompany.com/log”;*/
/* The following puts the file on the /tmp folder on the system root */
/*&URL_ID = “ftp://pmilind:password@mycompany.com/../../../../tmp”;*/

While &FNAMES.Len > 0
&MYFILE = GetFile(&FNAMES.Shift(), “R”, %FilePath_Absolute);

/* Catch each file */
&LogFile.WriteLine(“File No: “ | &file_no | “, File Name: “ | &MYFILE.Name);

   /* String processing to prepare parameters for putattachment() */
   &Date_Time = DateTimeToLocalizedString(%Datetime, “yyyyMMdd_HHmmss”);
   &ATTACHUSERFILE = &MYFILE.Name;

/* Actual File Name with path */
   &fname = Substitute(&MYFILE.Name, “:\”, “_”);
   &fname = Substitute(&fname, “\”, “_”);
   &fname = Substitute(&fname, “/”, “_”);
   &fname = Substitute(&fname, ” “, “_”);
   &ATTACHSYSFILENAME = Replace(&fname, Len(&fname) – 4, 0, “_” | &Date_Time | “_” | &file_no);

/* Sysfilename for DB purpose ? should be unique – use 4 if extension is 4 chars (docx), else use 3 (pdf) */
&LogFile.WriteLine(“&ATTACHUSERFILE = “ | &ATTACHUSERFILE);
&LogFile.WriteLine(“&ATTACHSYSFILENAME = “ | &ATTACHSYSFILENAME);

If Exact(Left(&URL_ID, 4), “URL.”) Then
   &return_code = PutAttachment(@(&URL_ID), &ATTACHSYSFILENAME, &ATTACHUSERFILE);

 /* Upload each file */
Else
&return_code = PutAttachment(&URL_ID, &ATTACHSYSFILENAME, &ATTACHUSERFILE);

/* Upload each file */
End-If;

/* Note: AddAttachment() is not allowed in AE */
&LogFile.WriteLine(“&return_code = ” | &return_code);
&MYFILE.Close();
&file_no = &file_no + 1;
End-While;

/* Create Step 2 in Application Engine*/
/* Make Sure Step 1 has COMMIT:After Step */
/* Uploaded Files to be deleted from Folder for security reasons */
/* Begin – Delete files from Folder – K.S.Rama Naidu */
/*Local File &MYFILE, &LogFile;
Local array of string &FNAMES;
Local number &file_no;
Local string &ATTACHUSERFILE, &ATTACHSYSFILENAME;*/
&LogFile = GetFile(“C:\temp\q_fileupload_log.log”, “w”, “a”, %FilePath_Absolute);

/* Set Log File */
/* CHANGE ACTUAL PATH IN PRODUCTION – Create a new URL to avoid hardcode – or pass from Run Control Page*/
/*
&FNAMES = FindFiles(URL.NEW_URL, %FilePath_Absolute);
*/
&FNAMES = FindFiles(“\\sy-hr9upg\hrdev\ps\test1\*.docx”, %FilePath_Absolute);
&LogFile.WriteLine(“No. of files found: ” | &FNAMES.Len);
&LogFile.WriteLine(” “);
&file_no = 1;
While &FNAMES.Len > 0
/* Catch each file */
&MYFILE = GetFile(&FNAMES.Shift(), “R”, %FilePath_Absolute);
&LogFile.WriteLine(“File No: ” | &file_no | “, File Name: ” | &MYFILE.Name);

/* DELETE each file */
&MYFILE.Delete();
REM &return_code = DeleteAttachment(URL.NEW_URL, &ATTACHSYSFILENAME);

&LogFile.WriteLine(“&return_code = ” | &return_code);
&LogFile.WriteLine(” “);
&MYFILE.Close();

&file_no = &file_no + 1;
End-While;

/* The above deletes files from source area*/
/* The following deletes file from the FTP or Database Server.
&ATTACHSYSFILENAME = “Test123.pdf”;
If Exact(Left(&URL_ID, 4), “URL.”) Then
&return_code = DeleteAttachment(@(&URL_ID), &ATTACHSYSFILENAME);
Else
&return_code = DeleteAttachment(&URL_ID, &ATTACHSYSFILENAME);
End-If;
*/

/* Use the following code in the main PeopleCode to schedule this app engine directly
&processRqst = CreateProcessRequest();
&processRqst.RunControlID = %Datetime;
&processRqst.ProcessType = “Application Engine”;
&processRqst.ProcessName = “YourAEName”;
&processRqst.RunDateTime = %Datetime;
&processRqst.RunLocation = “PSNT”;
&processRqst.Schedule();

If &processRqst.Status = 0 Then
WinMessage(“Success”, 0);
Else
WinMessage(“Error”, 0);
End-If;

*/

References:
http://www.pscustomizer.com/peoplesoft-examples/attachments/
http://peoplesoft.ittoolbox.com/groups/technical-functional/peopletools-l/write-file-into-database-using-sqr-2528747
https://forums.oracle.com/forums/thread.jspa?messageID=10320192
http://docs.oracle.com/cd/E15645_01/pt850pbr0/eng/psbooks/tpcl/book.htm?File=tpcl/htm/tpcl02.htm#H4541
http://docs.oracle.com/cd/E15645_01/pt850pbr0/eng/psbooks/tpcd/chapter.htm?File=tpcd/htm/tpcd11.htm
http://peoplesoftstuff.blogspot.com/2012/07/upload-file-from-local-to-server.html
http://peoplesoft.ittoolbox.com/groups/technical-functional/peopletools-l/browse-functionality-on-run-control-page-819632

Posted in Peoplecode.