Author Topic: REXX help - check to see if file is present.  (Read 4932 times)

Greggory Shaw

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 36
  • -Receive: 19
  • Posts: 416
    • View Profile
REXX help - check to see if file is present.
« on: February 27, 2016, 11:32:01 pm »
REXX help -  I need this for several programs !

How would you check to see if file is present in a directory and skip over-writing it (FIREFOX!.ENV), and/or more importantly backup it (renaming it) ?

Thanks

Greggory



Martin Iturbide

  • OS2World NewsMaster
  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 255
  • -Receive: 49
  • Posts: 1708
  • Your Friend Wil Declares...
    • View Profile
    • Martin's Personal Blog
Re: REXX help - check to see if file is present.
« Reply #1 on: February 28, 2016, 04:07:08 am »
Hi

I'm sorry I can not help you with REXX.
The only thing that I got is this repository where I tried to put together all REXX script I was able to find at hobbes.
https://github.com/OS2World/CMD-Scripts

Maybe those can work for learning.

Regards
Martin Iturbide
OS2World NewsMaster
... just share the dream.

Remy

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 6
  • Posts: 92
    • View Profile
Re: REXX help - check to see if file is present.
« Reply #2 on: February 28, 2016, 08:06:44 pm »
Hi,

Just use the following commad:

stream( file2chek, "c", "QUERY EXISTS" )
( if empty string then file does not exist )
 
"C" is for command

You can replace "QUERY EXISTS"
"QUERY SIZE" - interesting in cas the file exist but its size = 0 !

Best regards.

Greggory Shaw

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 36
  • -Receive: 19
  • Posts: 416
    • View Profile
Re: REXX help - check to see if file is present.
« Reply #3 on: February 28, 2016, 08:39:59 pm »
Hi,

Just use the following commad:

stream( file2chek, "c", "QUERY EXISTS" )
( if empty string then file does not exist )
 
"C" is for command

You can replace "QUERY EXISTS"
"QUERY SIZE" - interesting in cas the file exist but its size = 0 !

Best regards.

Thanks for the info Remy & Johnny works ! Added to FirefoxDD & Media Pack for OS/2.

@ Martin thx for the work on the collection of REXX code. I'll check it out for other things too


Greggory
« Last Edit: February 28, 2016, 09:13:47 pm by Greggory Shaw »

Greggory Shaw

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 36
  • -Receive: 19
  • Posts: 416
    • View Profile
Re: REXX help - check to see if file is present.
« Reply #4 on: March 04, 2016, 01:22:29 am »
Some more Rexx help,

Does some one know how to take user iinput (directories) and write a config file ?


Greggory

Jan-Erik Lärka

  • Global Moderator
  • Full Member
  • *****
  • Thank You
  • -Given: 5
  • -Receive: 11
  • Posts: 151
    • View Profile
Re: REXX help - check to see if file is present.
« Reply #5 on: March 04, 2016, 11:03:06 am »
Some more Rexx help,

Does some one know how to take user iinput (directories) and write a config file ?


Greggory

Split the problem up into parts and solve each part.
I prefer UZUnzipToVar or LINEIN below to load the settings and the latter is better in combination with 3. to write configuration settings.

1. Read a File into memory in one chunk that you need to interpret each setting later?
Code: [Select]
rxReadFile: PROCEDURE
  f_name = STRIP( ARG(1) )
  IF LENGTH( f_name )  = 0 THEN RETURN 1
  IF STREAM( f_name, 'C', 'QUERY EXIST' ) < 10 THEN RETURN 2
  f_size = STREAM( f_name, 'C', 'QUERY SIZE' )
  temp = CHARIN( f_name, 1, f_size )
  CALL STREAM f_name, 'C', 'CLOSE'
RETURN temp

or UZUnzipToVar to use a zipped textfile with key = value pairs of data
see Rexx Tips & Tricks documentation

or line by line and Interpret code directly to overwrite variables already populated with default settings¹
Code: [Select]
rxLoadSettings:
  f_name = STRIP( ARG(1) )
  IF LENGTH( f_name )  = 0 THEN RETURN 1
  IF STREAM( f_name, 'C', 'QUERY EXIST' ) < 10 THEN RETURN 2
  DO WHILE LINES( f_name ) > 0
    INTERPRET LINEIN( f_name )
  END
  CALL STREAM f_name, 'C', 'CLOSE'
RETURN 0

Code: [Select]
/* Only needed if you use dynamic variables that contain parameters */
rxGetSetting:
   INTERPRET 'temp = 'ARG(1)
RETURN temp

2. User input
Code: [Select]
/* The user has to type in a valid path, or 0 to skip and let us decide what to do */
DO WHILE answer = ''
  PARSE PULL answer
  /* If the user typed in anything, check it */
  IF answer = 0 THEN LEAVE /* User only typed 0, so we jump out of this loop and have to take care of it further down */
  ELSE IF STRIP( answer ) <> '' THEN
    answer = DIRECTORY( answer ) /* The path will be returned if it is valid, otherwise '' (empty string) will be returned */
END

Code: [Select]
IF answer = 0 THEN
   answer = DIRECTORY() /* We use the current directory instead?*/
/* another alternative is to RETURN  from the program, but that could be done in the loop as well. You decide*/

3. Write the config file
Code: [Select]
/* The example below write the  settings to file with current values */
settings = 'variablename search_path app_path combined_var'
CALL SysFileDelete my_cfg_file
DO i = 1 TO WORDS( settings )
  CALL LINEOUT my_cfg_file, SUBWORD( settings, i, 1 )' = "'VALUE( SUBWORD( settings, i, 1 ) )'"'
END


Variables such as ARG(1), ARG(2) etc. only appear in prewritten config/translation files.
Static text appear such as the variable "search_path" below is present in config files written by the application to save paths etc. related to the computer the script execute on.

¹) plain text file or .zip-file
variablename = "'This is a text with a parameter value ( 'ARG(2)' ) that can be set during execution'
app_path = "'C:\Tools\'ARG(2)"
search_path = "This is just a plain static text"
combined_var = "ARG(2)' and 'ARG(3)' can be used, as well as 'ARG(4)' and so on"

Load and overwrite default variables with
Code: [Select]
CALL rxLoadSettings 'mysettings.cfg'
/***** Not needed for value based only settings *****/
and get each variable combined with input parameters with
Code: [Select]
SAY rxGetSetting( 'variablename', 'MYPARAM' ) /*Would output "'This is a text with a parameter value ( MYPARAM ) that can be set during execution" */
It's possible to call rxGetSettings with more variables.

Greggory Shaw

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 36
  • -Receive: 19
  • Posts: 416
    • View Profile
Re: REXX help - check to see if file is present.
« Reply #6 on: March 05, 2016, 01:45:27 am »
Thank you very much Jan-Erik !

Getting to work on it now :)


Greggory
« Last Edit: March 06, 2016, 09:58:16 pm by Greggory Shaw »

Greggory Shaw

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 36
  • -Receive: 19
  • Posts: 416
    • View Profile
Re: REXX help - check to see if file is present.
« Reply #7 on: March 15, 2016, 06:53:48 pm »
Parsing - directory path (remove the last part in directory path ) ?


Code: [Select]
eJava = 'E:\Projects\eJava\eJava-008

NewPath = eJava
parse var eJava NewPath . "\" LeftoverVar 

Results remove after the first '\'

E:
Projects\eJava\eJava-008


Question: How to remove the '\eJava-008' last part of the path ?

eJava = 'E:\Projects\eJava

I've searched google and there's no examples (using paths), looking over string functions now.

Something like this may work, but then still how do you know when the last '\' is ???

Code: [Select]
/* REXX */                                                         
                                                                   
say "Enter path name"                                             
pull path                                                         
slash = '\'                                                       
parse value path with (slash) fname0 (slash) fname1 (slash) fname2,
     (slash) fname3 (slash) fname4 (slash) fname5 (slash) fname6,
     (slash) fname7 (slash) fname8 (slash) fname9 (slash)         
                                                                   
say fname0                                                         
say fname1                                                         
say fname2                                                         
say fname3                                                         
say fname4                                                         
say fname5                                                         


Or using a loop ?

Code: [Select]
+----------------------------------------------------------------------+
| -- pex1.nrx                                                          |01
| --                                                                   |02
| list = 'MARTIN DAVID BOB PETER JEFF'                                 |03
| i = 0                                                                |04
| loop while list <> ''                                                |05
|   parse list item list                                               |06
|   i = i+1                                                            |07
|   say i.right(2,'0') item.left(10) list                              |08
| end                                                                  |09
| exit 0                                                               |10
+----------------------------------------------------------------------+
                                                                pex1.nrx


Thanks

Greggory
« Last Edit: March 15, 2016, 07:13:56 pm by Greggory Shaw »

Remy

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 6
  • Posts: 92
    • View Profile
Re: REXX help - check to see if file is present.
« Reply #8 on: March 15, 2016, 08:23:00 pm »
Hi,

First be sure only have '\' and no '/'. You can use a translate(string,'\','/') to be sure all '/' are '\'
Then, you can have a look on lastpos()
e.g. lp = lastpos('\',string')

with string = E:\Projects\eJava\eJava-008, lp = 18

followed by a substr() function
e.g. path=substr(string,1,lp-1)

Or all together :
path=substr(string,1,lastpos('\',translate(string,'\','/'))-1)

Greggory Shaw

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 36
  • -Receive: 19
  • Posts: 416
    • View Profile
Re: REXX help - check to see if file is present.
« Reply #9 on: March 15, 2016, 08:38:40 pm »
Hi,

First be sure only have '\' and no '/'. You can use a translate(string,'\','/') to be sure all '/' are '\'
Then, you can have a look on lastpos()
e.g. lp = lastpos('\',string')

with string = E:\Projects\eJava\eJava-008, lp = 18

followed by a substr() function
e.g. path=substr(string,1,lp-1)

Or all together :
path=substr(string,1,lastpos('\',translate(string,'\','/'))-1)


Thank you Remy, Jan-Erik and Johnny for all the help !!!

I needed this for a couple othre programs also.


Greggory

Jan-Erik Lärka

  • Global Moderator
  • Full Member
  • *****
  • Thank You
  • -Given: 5
  • -Receive: 11
  • Posts: 151
    • View Profile
Re: REXX help - check to see if file is present.
« Reply #10 on: March 16, 2016, 03:23:51 pm »
I'd use

result = STRIP( FILESPEC( 'D', f_name )FILESPEC( 'P', f_name ), 'T', '\' )

even if the variable f_name only contain a path without the actual file name, as FILESPEC( 'N', f_name ) extract the last part such as the file name or last folder in path.

I use STRIP( ..., 'T', '\' ) to get rid of the trailing slash (if needed).

Greggory Shaw

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 36
  • -Receive: 19
  • Posts: 416
    • View Profile
Re: REXX help - check to see if file is present.
« Reply #11 on: March 17, 2016, 06:26:54 am »
I'd use

result = STRIP( FILESPEC( 'D', f_name )FILESPEC( 'P', f_name ), 'T', '\' )

even if the variable f_name only contain a path without the actual file name, as FILESPEC( 'N', f_name ) extract the last part such as the file name or last folder in path.

I use STRIP( ..., 'T', '\' ) to get rid of the trailing slash (if needed).

Thanks Jan-Erik !

Also, I've been looking for the 'get rid of the trailing slash' too :)


Greggory

Vadim Priluzkiy

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 5
  • Posts: 20
    • View Profile
    • osFree -- L4 microkernel based, open source OS/2 clone
Re: REXX help - check to see if file is present.
« Reply #12 on: March 18, 2016, 11:36:06 pm »
Parsing - directory path (remove the last part in directory path ) ?



Or using a loop ?

Code: [Select]
+----------------------------------------------------------------------+
| -- pex1.nrx                                                          |01
| --                                                                   |02
| list = 'MARTIN DAVID BOB PETER JEFF'                                 |03
| i = 0                                                                |04
| loop while list <> ''                                                |05
|   parse list item list                                               |06
|   i = i+1                                                            |07
|   say i.right(2,'0') item.left(10) list                              |08
| end                                                                  |09
| exit 0                                                               |10
+----------------------------------------------------------------------+
                                                                pex1.nrx


Thanks

Greggory

I am start develop RxMacroLib framework. My example for MkDirBranch function:
Code: [Select]
/* Make Directory branch                        */
/*                                              */
/* Example:                                     */
/*     D:\test is empty                         */
/* result=MkDirBranch('D:\test\data\test\1')    */
/* (result = 0 if Okay)                         */
/*     D:\test\data\test\1 maked if all ok      */

MkDirBranch:
Parse arg Path

RxM = RxMStemInit()
Interpret RxM

Chroot = Directory()
/* Add tailed directory separator if not set    */

If Right(Path, 1) \= RxM.DirSep Then
   Path = Path||RxM.DirSep

/* If FULL path or from root drive path is      */
/* not specified,  use current directory as     */
/* root for make branch                         */


Path.Drive = FileSpec('Drive', Path)
If Path.Drive = '' Then
   If Left(Path, 1) = RxM.DirSep Then
      Path.Drive = FileSpec('Drive', Chroot)
   Else
        Path.Drive = Chroot||RxM.DirSep
Path.Path = FileSpec('Path', Path)
Path = Path.Drive||Path.Path

/* Make Branch                                  */

CurDir = ''
Do While Path \= ''
   Parse Value Path with CurSubDir Value(RxM.DirSep) Path
   CurDir = CurDir||CurSubDir
   Call SysFileTree CurDir, 'IsExistDir', 'DO', '*****', '*****'
   If IsExistDir.0 = 0 Then
      Call SysMkDir CurDir
   CurDir = CurDir||RxM.DirSep
End
Return Result

This function parse absolute(z:\dir0\dir1\dir2\dir3\), "from root" on current disk(\dir0\dir\dir2\dir3) and relative(from current directory dir0\dir1\dir2\dir3) paths, and make directories if not exist. Also format of path string maybe with or without trailing slash.

PS: I am fix little error, and update message with latest GitHub code.
« Last Edit: March 19, 2016, 08:52:18 pm by Vadim Priluzkiy »
For instant messaging use Jabber JID: oxyd@jabber.ru

Greggory Shaw

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 36
  • -Receive: 19
  • Posts: 416
    • View Profile
Re: REXX help - check to see if file is present.
« Reply #13 on: March 19, 2016, 10:24:18 pm »
Thanks Vadim I'll check it out !

Greggory Shaw

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 36
  • -Receive: 19
  • Posts: 416
    • View Profile
Re: REXX help - check to see if file is present.
« Reply #14 on: November 23, 2016, 09:00:39 am »
Is there anyway to read the creation date of a file using Rexx and then rename that file adding the date ?

I want to use the file name Ziipboot_Current.zip

and, then change it to Zipboot_20161123.zip on the next backup run.


Or any other suggest on naming conventions for backup on images (Rsyncback/2).


Thanks,

Greggory
« Last Edit: November 23, 2016, 09:59:08 am by Greggory Shaw »