Login
Username:

Password:

Remember me



Lost Password?

Register now!
Sections
Who's Online
81 user(s) are online (53 user(s) are browsing Forums)

Members: 0
Guests: 81

more...
Support us!
Recent OS4 Files
OS4Depot.net



(1) 2 »


Help with Arexx Script
Just popping in
Joined:
2008/10/18 10:25
From Auckland, New Zealand
Posts: 62
Hi everyone,

I was hoping someone could give me some tips with an arexx script I'm working on.

I often receive or download archives that have files inside them that aren't in a directory. I have to create an archive to file the contents, so I wanted to write an arexx script that takes the archive name, snips off the last 4 characters (the suffix) and creates a folder with that name.

/* Create Folder*/

parse arg file drawer
address command

chars 
length(file)

newdir left(file,chars-4)

cd drawer
makedir newdir


I'm calling it from Filer with rx CreateDrawer.rexx {ou} {d} , where {ou} is the selected archive and {d} is the current dir.

The script works fine, however it gets broken by empty spaces in the filename. It looks like when it reaches a space in the filename, it stops reading, so "Customer images Sept 2019.zip" gets parsed as simply "Customer". Because it snips off the last 4 characters, if there's less than 4 characters in front of a space, the result is an error.

Does anyone know a way around this?

   Report Go to top

Re: Help with Arexx Script
Just popping in
Joined:
2007/12/19 12:36
From Finland
Posts: 28

   Report Go to top

Re: Help with Arexx Script
Quite a regular
Joined:
2009/4/28 3:57
From Adelaide, Australia
Posts: 904
@daveyw

Here's my quick and dirty fix (Example only. It doesn't cut the last 4 characters):

/**/
Parse Arg Args

IF Left(Args,1)='"' THEN Parse Var Args '"'File'" 'Drawer
ELSE Parse Var Args File' 'Drawer

Drawer
=Strip(Drawer,,'"')

SAY File
SAY Drawer
Exit


6.RAM Disk:> rx test "1 2" 2
1 2
2
6.RAM Disk:> rx test "1 2" "2 3"
1 2
2 3
6.RAM Disk:> rx test 1 2 3
1
2 3
6.RAM Disk:> rx test 1 "2 3"
1
2 3

   Report Go to top

Re: Help with Arexx Script
Just can't stay away
Joined:
2008/1/6 17:56
From Pennsylvania, USA
Posts: 1289
@daveyw
It is irritating when you extract an archive and discover that it didn't extract into a directory but mixed all the archive files with existing files in the destination directory. Here is an AmigaDOS script that should create a directory from file & directory arguments:
Create new drawer in existing drawer from filename

.key file/A,drawer/A
.bra {
.
ket }

If 
NOT EXISTS {drawerNOREQ
    
Echo "Destination directory not found"
    
Skip error
Endif

Set dirname `cut "{file}" word=1 separator="."`

Set path `Pathpart ADD {drawer} "$dirname"`

Makedir $path

LAB error
Unset dirname
Unset path


You might get better control by writing a C language utility that uses xadmaster.library to check for a "container" directory, create a directory if necessary and extract the archive with xadmaster.library.

_________________
Amiga X1000 with 2GB memory & OS 4.1FE + Radeon HD 5450

   Report Go to top

Re: Help with Arexx Script
Just popping in
Joined:
2008/10/18 10:25
From Auckland, New Zealand
Posts: 62
Thank you for the replies. This is what I've landed on.

/* Create Drawer V2*/ 

Parse Arg Args 

IF Left(Args,1)='"' THEN Parse Var Args '"'File'" 'Drawer 
ELSE Parse Var Args File' 'Drawer 

Drawer
=Strip(Drawer,,'"'
chars length(File)
newdir left(File,chars-4)
newdir2 space(newdir,1,'_')

address command

cd Drawer
makedir newdir2
Exit


Is also replaces the spaces with underscores.

@xenic

I've thought about expanding it so it checks that it's an archive, checks whether or not is has an existing directory, and even launching unarc to finish the job.

I don't know C though.

   Report Go to top

Re: Help with Arexx Script
Just can't stay away
Joined:
2006/11/24 18:52
From Gloucestershire, UK.
Posts: 1148
@daveyw

just for a quick comparison, here is a python script to do the same thing.

This method will create a complete dest path recursivly and if you cd to the archive drawer before running the script and uncomment the path and unarc lines it will extract the archive.

There is a simple check of the archive suffix, add more to line 7 if you want to. gzip, bzip etc. are covered by the 'zip' entry so no need to add them.

#!C:Python
#$VER:CreateDrawer.py V0.2 (19-09-2018)

import ossys
legal 
= (':','/')
#path = os.getcwd()
suffix = ('lha','lzx','zip','7z','rar','tar')

numargs len(sys.argv)
if 
numargs <> : exit()

arc sys.argv[1]
arc arc.lower()
if 
not arc.endswith(suffix):
    print 
"Unknown archive suffix, add to line 7 if you are sure it's an archive."
    
exit()

dest sys.argv[2]
if 
dest.endswith(legal):
    
newdest dest+arc[:-4]
else:
    
newdest dest+"/"+arc[:-4]

try:
    
os.makedirs(newdest)
except OSError:
    
# Destination drawer already exists.
    
pass

#os.system('appdir:unarc ' + path + arc + ' ' + newdest + '/' + ' AUTO')
exit()


Edit:
Added archive check and unarc stuff and fixed a few typos.

If you cut'n'paste this make sure the indentation is kept, all indented lines start with a single tab that will have been changed to a space by the site. a single space will still work but a tab makes it easier to read.


Edited by Severin on 2018/9/19 4:12:10
Edited by Severin on 2018/9/19 4:13:12
Edited by Severin on 2018/9/19 4:14:51
Edited by Severin on 2018/9/19 4:16:11
Edited by Severin on 2018/9/19 4:41:09
Edited by Severin on 2018/9/19 4:47:49
Edited by Severin on 2018/9/19 4:51:05
Edited by Severin on 2018/9/19 23:59:32
_________________
Amiga user since 1985
AOS4, A-EON, IBrowse & Alinea Betatester

Ps. I hate the new amigans website. <shudder>
   Report Go to top

Re: Help with Arexx Script
Just can't stay away
Joined:
2008/1/6 17:56
From Pennsylvania, USA
Posts: 1289
@Severin
It may look like the tabs are replaced by spaces but it's actually replacing the tabs with 0xA0 (160 decimal). I can open a Hex window in Turbotext and see what those blank spaces really are. Python might ignore those characters but if it's C code, GCC chokes on those characters. I just perform a "Find & Change" on those characters in Turbotext.


_________________
Amiga X1000 with 2GB memory & OS 4.1FE + Radeon HD 5450

   Report Go to top

Re: Help with Arexx Script
Just can't stay away
Joined:
2006/11/24 18:52
From Gloucestershire, UK.
Posts: 1148
@xenic

I've edited the script again and replaced the tabs/0xA0 chars with 4 spaces, it should just copy'n'paste now.

_________________
Amiga user since 1985
AOS4, A-EON, IBrowse & Alinea Betatester

Ps. I hate the new amigans website. <shudder>
   Report Go to top

Re: Help with Arexx Script
Just popping in
Joined:
2008/10/18 10:25
From Auckland, New Zealand
Posts: 62
Hmm, I get
File "python:scripts/CreateDrawer.py"line 2
SyntaxError
Non-ASCII character 'xa0' in file python:scripts/CreateDrawer.py on line 2but no encoding declaredsee http://www.python.org/peps/pep-0263.html for details

So I guess there's still a copy paste problem.

   Report Go to top

Re: Help with Arexx Script
Just can't stay away
Joined:
2008/1/6 17:56
From Pennsylvania, USA
Posts: 1289
@daveyw
It appears that most of the tabs & spaces in a "code" block are replaced by the 0xA0 code. It's ironic that indentation can only be preserved by using a code block but copying text from a code block won't compile

If you want to copy & test most program code from an Amigans code block, you will need to find & replace 0xA0 codes with a capable text editor or a HEX editor like FileX from OS4Depot.


_________________
Amiga X1000 with 2GB memory & OS 4.1FE + Radeon HD 5450

   Report Go to top

Re: Help with Arexx Script
Just can't stay away
Joined:
2006/11/24 18:52
From Gloucestershire, UK.
Posts: 1148
@daveyw

Try this version... Paste it into an something like notepad and do a search and replace to replace the $'s With spaces.

#!C:Python
#$VER:CreateDrawer.py_V0.2_(19-09-2018)

import_os,_sys
legal_
=_(':','/')
#path_=_os.getcwd()
suffix_=_('lha','lzx','zip','7z','rar','tar')

numargs_=_len(sys.argv)
if_numargs_<>_3_:_exit()

arc_=_sys.argv[1]
arc_=_arc.lower()
if_not_arc.endswith(suffix):
____print_"Unknown_archive_suffix,_add_to_line_7_if_you_are_sure_it's_an_archive."
____exit()

dest_=_sys.argv[2]
if_dest.endswith(legal):
____newdest_=_dest+arc[:-4]
else:
____newdest_=_dest+"/"+arc[:-4]

try:
____os.makedirs(newdest)
except_OSError:
____#_Destination_drawer_already_exists.
____pass

#os.system('appdir:unarc_'_+_path_+_arc_+_'_'_+_newdest_+_'/'_+_'_AUTO')
exit()



Edited by Severin on 2018/9/29 22:07:38
_________________
Amiga user since 1985
AOS4, A-EON, IBrowse & Alinea Betatester

Ps. I hate the new amigans website. <shudder>
   Report Go to top

Re: Help with Arexx Script
Home away from home
Joined:
2006/11/26 21:45
From A haunted Castle somewhere in the Bavarian Mountains
Posts: 3133
@daveyw

I'm going to hijack your thread

@all

I have two questions:

1) Is it possible to "jump" in an AREXX script, like it's done in batch scripts, back and forth in a loop or is there no way?

2) Is it possible to use local macros (preferrably written by me)
I know that there is this CALL() command which can take other (internal or external) commands and process them.

Would it be possible to write a macro after the EXIT line in a script and call that macro from within a loop?

If not, would such a thing be possible and how would one achieve that?

3) Bonus question
IF THEN loops don't use END as far as i know? (Only DO WHILE loops do)
Why isn't the parser warning (or even erroring out) me about using an END in a IF THEN loop?

Don't tell me because it's too old and because there is no source and because it won't be updated and i should use perl instead!

_________________
If slaughterhouses had glass walls, everyone would be a vegetarian. ~ Sir Paul McCartney
-
Did everything just taste purple for a second? ~ Philip J. Fry
-
Ain't got no cash, ain't got no style, ladies vomit when I smile. ~ Dr. John Zoidberg
   Report Go to top

Re: Help with Arexx Script
Just popping in
Joined:
2008/10/18 10:25
From Auckland, New Zealand
Posts: 62
@Raziel

That's OK by me!

@Severin

Sorry, I'm still getting the same error. Perhaps upload to OS4 Depot?

   Report Go to top

Re: Help with Arexx Script
Just can't stay away
Joined:
2006/11/24 18:52
From Gloucestershire, UK.
Posts: 1148
@daveyw

Ok, I've checked and it seems every space as well as tabs are being replaced with ascii 160. As there is a $ thats needed for the version string I've replaced all spaces with underscore so cut'n'paste then replace all "_" with " " and it should work.

_________________
Amiga user since 1985
AOS4, A-EON, IBrowse & Alinea Betatester

Ps. I hate the new amigans website. <shudder>
   Report Go to top

Re: Help with Arexx Script
Just can't stay away
Joined:
2006/12/1 18:01
From Copenhagen, Denmark
Posts: 1065
@Raziel

1. There are much better means of control than simple jumps, for instance subroutine calls, do/while loops, for loops etc. etc.

2. Yes, you can call both internal routines and external scripts (stored in independant files), if that's what you mean. But you already know that, it seems. What do you mean by macros, if not that?

3. IF/THEN(/ELSE) only take one statement in each branch. However, that statement can itself be a DO/END block.

So you would write e.g.

IF something
THEN DO
something
something more
END

Bonus answer: All this, and much more about the ARexx language, is easily found e.g. in http://aminet.net/package/util/rexx/ARexxGuide2_0A - which I highly recommend.

The original ARexx manual isn't half bad either, but can be hard to find nowadays.

Best regards,

Niels

   Report Go to top

Re: Help with Arexx Script
Just can't stay away
Joined:
2006/11/24 18:52
From Gloucestershire, UK.
Posts: 1148
@Raziel

inreverse order:

Q3:
The ARexx equivalent can be used in one of these ways:-

Format 1:-

IF expression THEN instruction clause
... rest of program

Format 2:-

IF expression THEN instruction clause 1
ELSE instruction clause 2
... rest of program

Format 3:-

IF expression THEN DO
instruction clause 1
instruction clause 2
instruction clause 3
END
ELSE DO
instruction clause 4
instruction clause 5
END
... rest of program


Q 2 & 1:

Arexx has a gosub equivalent that also wirks as a statement or function like in blitz that uses the call command:

using calls after exit() is no problem, you can have multiple exit() commands in a sript, think of it like the dos script quit command.

main:
if dog = awake then call feedit /*used as a gosuub*/
else call leavealone
call printmsg('finished') /*used as a statement*/
exit()

feedit:
rem give dog food
return

leavealone:
rem ignore dog
return 0

printmsg:
say Arg(1)
return 0

return can be a number or variable, using a variable is the equivelent of a function eg:

a=5
call square(a)
say a
exit()

square:
number=Arg(1)
ans = number^2
return ans

or simpler:

square:
return Arg(1)^2

_________________
Amiga user since 1985
AOS4, A-EON, IBrowse & Alinea Betatester

Ps. I hate the new amigans website. <shudder>
   Report Go to top

Re: Help with Arexx Script
Home away from home
Joined:
2006/11/26 21:45
From A haunted Castle somewhere in the Bavarian Mountains
Posts: 3133
@Severin
@nbache

Thanks guys

_________________
If slaughterhouses had glass walls, everyone would be a vegetarian. ~ Sir Paul McCartney
-
Did everything just taste purple for a second? ~ Philip J. Fry
-
Ain't got no cash, ain't got no style, ladies vomit when I smile. ~ Dr. John Zoidberg
   Report Go to top

Re: Help with Arexx Script
Just can't stay away
Joined:
2008/1/6 17:56
From Pennsylvania, USA
Posts: 1289
@Raziel

Although others have suggested a "gosub" type command as a response to question number 1, there is an AREXX equivalent of a "goto" command. You can use the AREXX "SIGNAL" command to jump to another section of code.

Simplistic example:

/* Intialize Variables */
ScriptErr = 10 /* Default Error Return */
TestValue = 99

IF TestValue > 10 THEN SIGNAL Depart

SAY "Continuing script"
/* Other script stuff here */
ScriptErr = 0 /* Script succeeded */

Depart:
IF ScriptErr > 0 THEN SAY "Error: Script failed"

_________________
Amiga X1000 with 2GB memory & OS 4.1FE + Radeon HD 5450

   Report Go to top

Re: Help with Arexx Script
Home away from home
Joined:
2006/11/26 21:45
From A haunted Castle somewhere in the Bavarian Mountains
Posts: 3133
@xenic

Nice, just what i was looking for, thank you

_________________
If slaughterhouses had glass walls, everyone would be a vegetarian. ~ Sir Paul McCartney
-
Did everything just taste purple for a second? ~ Philip J. Fry
-
Ain't got no cash, ain't got no style, ladies vomit when I smile. ~ Dr. John Zoidberg
   Report Go to top

Re: Help with Arexx Script
Just can't stay away
Joined:
2006/12/1 18:01
From Copenhagen, Denmark
Posts: 1065
@Severin

Quote:
return can be a number or variable, using a variable is the equivelent of a function eg:

a=5
call square(a)
say a
exit()

square:
number=Arg(1)
ans = number^2
return ans

or simpler:

square:
return Arg(1)^2
I think you may have been a little too hasty in that last example, Severin.

First, the ^ operator stands for the logical operator "exclusive or". The "power" operator in ARexx is a double asterisk (**).

Second, your example fails to demonstrate using the result of a function call, since it is never assigned in the calling code. The printout would be 5, not 25, as one would probably expect.

Finally (detail), it probably doesn't make much sense to put an empty set of parentheses after exit. You can put a value in such parentheses, which will return that value as a return code to the calling environment, but if you don't want to do that, just leave out the parentheses.

The following would be better:

/* TestSquare.rexx */
a=5
b
=square(a)
say b
exit

square:
num=Arg(1)
ans=num**2
return ans


Best regards,

Niels

   Report Go to top


(1) 2 »



[Advanced Search]


Powered by XOOPS 2.0 © 2001-2016 The XOOPS Project