English Amiga Board


Go Back   English Amiga Board > Coders > Coders. System > Coders. Scripting

 
 
Thread Tools
Old 21 September 2022, 23:08   #1
EctoOne
Registered User
 
EctoOne's Avatar
 
Join Date: Jun 2020
Location: Germany
Posts: 370
Thumbs up [Solved] How can I determine if the current path is a drive or a directory?

Is there any way to determine whether the current path is a directory or the root of a drive?

I need to set a path as a variable to set a target for copy operations.
I tried using CD but the end of output is different if it's a drive or a directory.
It is either
Code:
DRIVE: or DRIVE:DIRECTORY
And when I use something like this:
Code:
Set TARGET 'CD' 
Copy SOMEFILE to ${TARGET}NEWFILE
It works when the path is a drive but not when it's a directory. Then the new file ends up as
Code:
DRIVE:DIRECTORYNEWFILE
or whatever.
And I can't do ${TARGET}/NEWFILE because then it won't work when it's a drive.

To add more info, yes I need NEWFILE because I need to rename some files.
And I hope this would be possible to fix with just shell scripting. No additional software or arexx.

Last edited by EctoOne; 22 September 2022 at 01:48.
EctoOne is offline  
Old 21 September 2022, 23:38   #2
malko
Ex nihilo nihil
 
malko's Avatar
 
Join Date: Oct 2017
Location: CH
Posts: 4,856
Depending on your OS version, this can be an idea that need a bit of adaptation : https://eab.abime.net/showthread.php?t=108682 ?
malko is offline  
Old 22 September 2022, 01:45   #3
EctoOne
Registered User
 
EctoOne's Avatar
 
Join Date: Jun 2020
Location: Germany
Posts: 370
Thanks, using INFO to determine what the current path is works. Don't know how safe this is, but I came up with this:

Code:
SET CWD `CD`
INFO >T:eoCWD ${CWD}
SEARCH >NIL: FROM T:eoCWD SEARCH Unit QUIET
IF WARN ; Directory
    SETENV eoTARGET "${CWD}/"
ELSE ; Drive
    SETENV eoTARGET "${CWD}"
ENDIF
DELETE >NIL: T:eoCWD
Whenever the current path is a directory, there is no output from INFO which means the string "Unit" can not be found in the temp file. This will add the / to the CD output.
Using "Unit" as search string might not be the best way, but since I'm not parsing any arguments, I couldn't use a more unique string. Unit is one of the strings that comes up in the output of INFO. And since there is no output if it's a directory, I don't think there will be any problem.
EctoOne is offline  
Old 22 September 2022, 07:29   #4
malko
Ex nihilo nihil
 
malko's Avatar
 
Join Date: Oct 2017
Location: CH
Posts: 4,856
malko is offline  
Old 22 September 2022, 08:58   #5
thomas
Registered User
 
thomas's Avatar
 
Join Date: Jan 2002
Location: Germany
Posts: 6,985
[QUOTE=EctoOne;1566047]I
And when I use something like this:
Code:
Set TARGET 'CD' 
Copy SOMEFILE to ${TARGET}NEWFILE

You could do it like this:

Code:
assign target: ""
copy somefile to target:newfile
assign target:
The name target: could be made unique by adding the process number to it. But this only works if the script has arguments:

Code:
.key dummy
assign target<$$>: ""
copy somefile to target<$$>:newfile
assign target<$$>:

Edit: you probably only used the current directory for the example, but if the target is the current directory, you don't need to supply a target path at all you could just do

Code:
copy somefile newfile
thomas is offline  
Old 22 September 2022, 10:35   #6
EctoOne
Registered User
 
EctoOne's Avatar
 
Join Date: Jun 2020
Location: Germany
Posts: 370
Thanks thomas, I normally try to avoid making assigns but your solution is so simple that I might use it. Because it also makes it easier to access sub dirs or at least it's better to read.
EctoOne is offline  
Old 22 September 2022, 15:44   #7
freakofnature
Registered User
 
Join Date: Jan 2021
Location: Darksideofthemoon
Posts: 69
A short version to check for directory or disk

Code:
cd >ram:test
search >nil: ram:test #?:[a-z,0-9] pattern
if warn
echo "is drive"
endif
have fun!

Last edited by freakofnature; 22 September 2022 at 17:30.
freakofnature is offline  
Old 22 September 2022, 17:14   #8
thomas
Registered User
 
thomas's Avatar
 
Join Date: Jan 2002
Location: Germany
Posts: 6,985
The first echo should probably be cd, shouldn't it?
thomas is offline  
Old 22 September 2022, 17:31   #9
freakofnature
Registered User
 
Join Date: Jan 2021
Location: Darksideofthemoon
Posts: 69
It should!
Corrected, thanks Thomas!
freakofnature is offline  
Old 22 September 2022, 18:17   #10
EctoOne
Registered User
 
EctoOne's Avatar
 
Join Date: Jun 2020
Location: Germany
Posts: 370
Wow, I didn't know that the Amiga had the support for this type of pattern matching.
EctoOne is offline  
Old 03 November 2022, 12:31   #11
EctoOne
Registered User
 
EctoOne's Avatar
 
Join Date: Jun 2020
Location: Germany
Posts: 370
Found another solution using ECHO instead of parsing a file and thought it might be worth sharing it.

The short version would be this:
Code:
SET cwd "`CD`"
IF "`ECHO "${cwd}" LEN 1`" EQ ":"
  ECHO "is drive"
ENDIF
A longer version using RequestFile to set a target is this:
Code:
SET eoTARGET `REQUESTFILE SYS: TITLE "Select install drive" DRAWERSONLY`
IF NOT "`ECHO "${eoTARGET}" LEN 1`" EQ ":"
  SET eoTARGET "${eoTARGET}/"
ENDIF
ECHO "${eoTARGET}"
It basically adds a / at the end of the selected path if it's not the root of a drive. Which makes ${eoTARGET} a complete path and can be used for file operations no matter what the real destination is.
EctoOne is offline  
Old 03 November 2022, 16:57   #12
freakofnature
Registered User
 
Join Date: Jan 2021
Location: Darksideofthemoon
Posts: 69
Nice try.

echo $var len 1 is unreliable on AOS3.0 to 3.9, as it fails on path names containing <space>, like "Ram Disk:". Returning string then is "m :", every last char taken from all parts.
It works on 3.2 as well as on older os with shell replacement like zshell, but not vinced.

Brackets are used to access parsed arguments. You can safely omit them on local vars.
freakofnature is offline  
Old 03 November 2022, 22:28   #13
EctoOne
Registered User
 
EctoOne's Avatar
 
Join Date: Jun 2020
Location: Germany
Posts: 370
Huh, didn't notice that because I have a line in my user-startup that relabels my ram disk to ram. Wouldn't it be possible to wrap it in additional quotes? I will look into that.

I know about the brackets but I'm so used to them from bash scripting, also don't you need them anyway if you want to directly add something? Like in my second example where I add the /:
Code:
SET eoTARGET "${eoTARGET}/"
At least on Linux this is required if I remember correctly because it wouldn't recognize the variable otherwise. Never really tried it without brackets for Amiga scripts.

Edit: I can't reproduce the behavior you described. It works fine on my regular installation and even when I run the script using just a Workbench 3.1 disk.

Last edited by EctoOne; 04 November 2022 at 00:46.
EctoOne is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading files from current directory in assembly hukka Coders. System 40 21 May 2021 12:14
Returning the current working floppy drive device mcgeezer Coders. Asm / Hardware 5 08 July 2019 21:38
vasm Get Current Work Directory (Windows expert needed) phx Coders. General 13 15 October 2018 20:57
Current options for an Amiga 500 hard drive carloratm support.Hardware 0 27 August 2018 21:16
Add directory to path with ARexx? Firestone Coders. Scripting 5 07 May 2017 04:28

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 17:48.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.12685 seconds with 13 queries