Guidance
指路人
g.yi.org
Guidance Forums / Reginald Rexx / Starting versus "work" directory

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. Starting versus "work" directory
#1519
Posted by: misi01 2003-01-02 23:49:45
I have a REXX script that resides in the H:reginaldFRED directory. But when the script runs, I want its "work directory" to be c:michael.

(A "work directory" is where I may create backup files, or data files, etc. It could be a different location than where the script resides).

But, if I call the DIRECTORY() function, it returns "H:reginaldFRED". (ie, When my script runs, the current directory is the directory where the script resides -- not my work directory).

So, how do I get my work directory, and how do I list the files in it?
Michael
Message2. Re: Starting versus
#1521
Posted by: Jeff Glatt 2003-01-03 02:28:00
Obviously, the program that launched your REXX script (be it RPC, or Reginald's REXX script launcher, or some other program capable of starting up a REXX script) set the current directory to the same directory as where your script resides. This is typical behavior. Normally, your "work directory" is the same as the directory where your script resides. And DIRECTORY() returns whatever the current directory is.

If you want your work directory to be something else, then how you get your "work" directory really depends upon how you've implemented this whole scheme. For example, is your work directory always going to be on the C drive, and named "michael"? If so, then you can hard-code this string right in your script.

Reginald has a MATCHNAME() function that lists the contents of a directory. (For other interpreters, there is no such built-in function. You have to call a shell command such as 'DIR' and capture the output of it. Ick. See my REXX book about those ugly details).

So to list the contents of the 'C:MICHAEL' directory, you'd do the following:
/* Repeat until MATCHNAME tells us to stop. */
DO UNTIL result = ""

   /* Have MATCHNAME return the next name. */
   result = MATCHNAME(, 'MyVar', 'C:Micheal', 'NCRAT')

   /* Did MATCHNAME return a name? */
   IF result = "" THEN DO

      /* Now SAY that name. */
      SAY myvar

   END /* MATCHNAME returned a name. */

END
That's all there is to it. You've hardwired the string 'C:Michael' in your MATCHNAME() call.




Now, if your work directory may change, then you need to figure out how you're going to set that work directory, and later retrieve it.

If your script is always going to be run via the user clicking on the script's own desktop icon, then note that you can set the "Start in" directory. Just right-click on the desktop icon to open its pop-up menu, and select "Properties". You'll see where to change the "Start in" directory. This will affect what the DIRECTORY() function returns when your script first starts. This also applies if you're using RPC to create a self-running script (EXE). This is a very easy way to set your working directory, and retrieve it with DIRECTORY(). And the user can change it himself just by changing the icon's properties.




Of course, if your script is being launched by some other program, then this setting doesn't necessarily take effect. So then, you have to resort to implementing your own "set/retrieve working directory" feature in your script. In that case, you need to decide how/where you're going to save the working directory name. In the registry? In that case, you could use Reginald's VALUE() function to create a registry file containing the name of the work directory, and also retrieve it when needed. Or, maybe you could use the Temp directory if your "work files" are only temporary. You could use Reginald's SEARCHPATH("%TEMP%") to find the name of the temp directory.

Assuming that you want a permanent "work directory", you could have some feature in your script that lets a user type in his desired work directory name. (Better yet, use REXX GUI's GUIFILE() BROWSE dialog to let him point and click his way to the desired path). Then assuming you have the work dir name in the variable 'MyWorkDir', and you wish to save this under the Current User's registry settings, under a folder named "My Script" and a file of 'Work', you could call this routine:
setworkdirectory:

DO

/* Create a dir named "My Script" under current
 * user's settings.
 */
VALUE("My Script", , "WIN32")

/* Create a file named "Work" in the above
  * dir, and set its contents to the name of
  * our work directory.
  */
VALUE("My ScriptWork", myworkdir, "WIN32")

CATCH SYNTAX
   CONDITION('M')
END
RETURN
When your script first starts, you can retrieve the work directory into the variable 'MyWorkDir' as so:
/* Read a file named "Work" in the "My Script"
  * dir of the current user's registry settings.
  */
myworkdir = VALUE("My ScriptWork", , "WIN32")

/* If it's an empty string, he hasn't set it yet. In
 * this case, use a default -- maybe the temp
 * directory.
 */
IF myworkdir = "" THEN myworkdir = SEARCHPATH("%TEMP%")
After the above, you could list the contents of MyWorkDir. You do it the same as my previous example of using MATCHNAME, except instead of passing the literal string "C:MICHAEL", you pass the value of the variable MyWorkDir like so:
result = MATCHNAME(, 'MyVar', myworkdir, 'NCRAT')
And that's basically how to best implement a work directory that can be different from the directory in which your script resides (and also be different for each user who logs in, by virtue of using the current user's registry settings to save your work dir name). And that's also how to list the contents of a directory using Reginald's MATCHNAME().
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Sat 2024-4-27  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0