Deleting unneeded files using a Rexx script

Cours deleting unneeded files using a Rexx script, tutoriel & guide de travaux pratiques en pdf.

A quick script

Any number of programming languages can make short work of this task. I like Rexx, a scripting language that combines power with ease of use. While these two goals normally conflict, Rexx includes a plethora of design techniques to combine them, including simple syntax, free-formatting, case-insensitivity, structured control and modularity, a tiny core of instructions surrounded by a large function set, easy extensibility, and other many features.
You might recall Rexx from the Windows Resource Kits for Windows 2000 and earlier. What you may not realize is that today there nine free and open source Rexx interpreters that run on every platform, from handhelds to mainframes. All Rexx interpreters meet the international Rexx standard and add extra features for particular purposes—like extensions for Windows or Linux programming, full object-orientation, Java compatibility, extra features for handhelds, UNIX functions, and the like. Rexx is a major scripting language with a strong base in the new Europe. Table A lists the free Rexx interpreters, their unique strengths, and where you can download them.
Let’s write the script. The code in Listing A is a standard Rexx script that runs under any of the Windows Rexx interpreters (such as Regina and Reginald), and implements a simple approach to identifying large files.
Take a look at the script. You’ll notice that Rexx is free format and case-insensitive. Space and capitalize code however you like. Adapt the language to your preferences. Comments appear between the characters /* and */ and may be placed on their own lines or intermingled within the code. I started this script with a comment block that defines its purpose.

Code analysis

Following the comment block, the first few lines of the program assign file names to the variables dir_file, sort_file, and out_file . The first is the file into which the dir command listing goes; the second is where the script writes its cleaned-up or « filtered » file list; and, the last is where the Windows sort command writes its sorted output.
The next line in the script shows how the script issues the Windows dir command. Rexx interprets a line of code, and whatever it does not understand as part of the Rexx language, it sends to the operating system (or any other specified environment), as a command:
‘dir c:\ /-c /s > ‘ dir_file /* List all files on machine */
I’ve enclosed the first portion of the dir command within single quotes, which prevents Rexx from evaluating the code before sending it to the operating system. In this case this is necessary because otherwise Rexx will try to interpret some of the symbols (like the >) incorrectly. But I did not enclose the reference to the variable dir_file in quotes, because I want Rexx to interpret this variable into its value (the file name given it in the initial assignment statement). So after evaluation, Rexx sends a command looking like this to the Windows command line:
dir c:\ /-c /s > xx_dir_list.txt
The script issues the dir command with the command’s /s flag to provide a full listing of all files in all directories on the drive. The /-c flag ensures that no commas appear in the file sizes, for easy sorting of the file list by size later on. Of course, you’ll get a slightly different file list from the dir command depending on which switches you use and your version of Windows. For the purposes of this program, it really doesn’t matter, as long as the script identifies the biggest files on the machine. You could also read in the drive letter to process, making this script more flexible, but I just hard-coded it as c:\ to simplify the example.
The dir command output is sent to the file identified by variable dir_file. Next, the script processes all the lines in that file in order to eliminate unneeded output. It accomplishes this by a simple do while loop, which uses the built-in function lines to determine whether there are lines in the input file to process:
do while lines(dir_file) > 0 /* Process the full file list */
Once inside the do while loop, the linein function reads one line of data from the file:
line = linein(dir_file) /* Read a line from file list */
Like lines, linein is a Rexx built-in function. Functions return values that are plopped right into the code where they occur. Rexx features a small instruction set surrounded by a large function library. The language is extensible in that there are many, many free function libraries you can plug into your code. Following an initial setup statement, scripts use any external function just like a built-in function. This provides a simple, consistent way to extend the power and functionality of the language without increasing its complexity.
The dir command output consists of a label providing a sub-directory name and a list of files in that sub- directory. An example appears is shown in Listing B. Our objective is to capture the name of each sub-directory, as well as the file names and sizes. We want to eliminate extraneous lines—for example, those that contain <DIR>, the summary lines, and any blank lines.

MORE SPACE
Typical Large Files
A QUICK SCRIPT
Table A: Free Rexx interpreters
Listing A
CODE ANALYSIS
Listing B
WHAT NEXT
ADDITIONAL RESOURCES
Version history
Tell us what you think

Cours gratuitTélécharger le cours complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *