Want to hire me? For availability and scheduling please email info@arcepm.com

17 January 2017

The Compleat Idiot's Guide to PBCS, No. 19 -- Delete Multiple Files with EPM Automate

Introduction

I have, somehow yet again convinced others to write my blog for me.  I must be getting good at this, or others must be losing their minds because this really good post came to Yr. most hmbl. & obt. svt. spontaneously.

Perhaps my mental anguish/neuroses/laziness re my awesome (ahem) and still incomplete (double ahem) PBCS rolling seven day backup post came through the ether to fellow geek Chris Rothermel.  

Or perhaps he fell under the hypnotic gesture of CameronMandrake The Magician and just wrote it ‘cos he was compelled to as shown below:
Mandrake_1986-08-17

Alas, I have neither the snazzy convertible (actually I do have a blue 1993 Miata MX-5 not altogether unlike Mandrake's) or the tophat (I do have one of these which are fantastic for keeping the deer ticks off in the woods along with this) or the cape or alas the trimmed moustache (or any moustache at all) or the power of the supernatural and hence the ability to make bad guys drop rifles on their heads which, given the CRACK, had to really hurt as evinced by the “OU!” grunts of the two goons.  I do however have this blog so there’s that; I’ll bet Mandrake doesn’t have one of those.

And with that run on sentence out of the way, take it away, Chris.

Delete multiple files with EPM Automate

The purpose of this document is to share a solution which allows for the deletion of multiple files using EPM Automate.  This solution simply extends the functionality of the deletefile command from deleting a single file to deleting multiple files with the assistance of a file list and recursive calls to the deletefile command.

The heart of the solution is found in lines 36 through 49 below where the deletefile command called for each record in the file.  Additional echo commands are used to display the activity of the job.
.
Error handling surrounds the core solution.  Distinct messages are reported if there was an error, if no filename was provided, or even if the file provided was empty.
.
.
.

Execution and Testing

Create a file to be used as the list of files to delete.  Using the listfiles command we find a list of valid files.

Four of the file names are selected and a fifth bogus filename is also saved in the filelist of files for deletion.

The batch script is invoked using the file “MyDeleteFile.txt” as the input.

Using the listfiles command again we confirm four of the requested files have been deleted.

Searching for a log file in this directory yields a log file for this error containing various details of the error.

When examined the file contains various debug statements for this error and the last line echoes the same error seen earlier.  A separate file is created for each file that has trouble processing.

Chris’ conclusion

Use this batch script to delete a list of files from the cloud.  Remember to first have an active session with the cloud server and to provide a file to the batch command.  Enjoy using this script and I hope it saves you time managing the files in your cloud environment.

Code

ea_deletefiles.bat

@echo off
REM ---------------------------------------------------------------------------
REM  ea_deletefiles.bat
REM
REM  Author:  Chris Rothermel
REM
REM  Date:    2017-01-12
REM
REM  Usage:      ea_deletefiles <filename containing one filename per row>
REM
REM  Purpose: This script recursively calls the deletefile command for each
REM           record in the file provided.  There are other checks on the
REM        file such as checking it exists and checking to see if there are
REM           any records in the file.
REM
REM  Input:    The input file should have no quotation marks and no trailing
REM           whitespace.  The record delimiter is a new row (<CR> <LF>).
REM
REM  Pre-Req:    This script assumes an epmautomate session is already in use.
REM           Use the epmautomate login command to initiate a session.
REM
REM ---------------------------------------------------------------------------

REM Reset %erorrlevel%
verify > nul

REM Check if a filename parameter has been provided
IF [%1]==[] goto :Usage
REM Check if the file exists
IF NOT EXIST "%1" goto :InvalidFile
REM   Check if the file is empty
for %%A in ("%1") do if %%~zA==0 goto EmptyFile

REM Recursively call deletefile for each record in the file
REM   Trim trim any left whitespace; beware whitespace on the right
REM   No quotes allowed in the records
REM   One filename per row
for /F "tokens=* usebackq delims= " %%a in (%1) do (
    call echo Deleting "%%a%%"
    call epmautomate deletefile "%%a%%")
IF %ERRORLEVEL% NEQ 0 goto :ERROR

REM * Successful Completion *
ECHO .
ECHO Requested epmautomate delete commands submitted
ECHO Verify and check for deletefile.log files
exit /b %errorlevel%

REM ---- Warnings and Errors ----
:EmptyFile
ECHO Warning:  No records found in the file provided

:Usage
REM Use the special %0 variable to get the path to the current file.
REM Write %~n0 to get just the filename part.
ECHO Usage: %~n0 FILENAME
ECHO You must have a session with EPM Automate active to use this.  Use the epmautomate login command.
exit /b %errorlevel%

:InvalidFile
ECHO Warning: File "%1" does not exist
exit /b 30

:ERROR
echo ERROR: %errorlevel%.
exit /b %errorlevel%

REM EOF

My conclusion

Yet again another excellent post from Chris.  We owe him a debt of gratitude for giving his knowledge away for free.  Yes, yes, that’s the point of this blog and others but you (we) ought to reflect on the community the EPM world has.  It is possible for people to be altruistic.  You see that in ODTUG and you see it here.  Thanks again, Chris.

Be seeing you.

No comments: