Author Topic: Question about using SHELL "dir" on non windows OS.  (Read 9871 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Question about using SHELL "dir" on non windows OS.
« Reply #45 on: February 06, 2021, 02:02:33 pm »
Are you saying you can't get access to clip data faster than you can get access file data?
Is clip data written to temp files on disk by OS, that would be an explanation about access being slower for clip but seems not very smart coding.

@bplus You aren't understanding what I am saying. "clip" is a command in command prompt. The action of piping "dir" or whatever into "clip" is what takes extra time because now you are calling two commands rather than one. The clipboard itself is fine. What you are doing in "dir | clip" is telling it to run "dir" and then send everything from "dir" into the command "clip" which THEN fills the clipboard. That's slow. That command is running TWO executables. "clip" is reading the stdout handle of "dir" and then writing to the clipboard. When you write straight to a file you aren't having to worry about running a second executable. That time spent is saved. WinAPI has functions for making temp files out in a temp directory and they work great.

Oh it's probably the SHELL that's slower than pipecom.

I've been saying that SHELL is slower than pipecom for months now. But it isn't JUST the SHELL that's slower here. The "clip" command makes it way slower than pipecom and even slower than using files. Use temp files instead of "clip".
« Last Edit: February 06, 2021, 03:46:12 pm by SpriggsySpriggs »
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Question about using SHELL "dir" on non windows OS.
« Reply #46 on: February 06, 2021, 04:30:31 pm »
OK working with what pipecom returns people should know it is Chr$(10) delimited string should they want to split the string into an array.

Also it appears an empty string$ = "" is on the end, don't know if that is from pipecom probably or the split I am using?

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Question about using SHELL "dir" on non windows OS.
« Reply #47 on: February 06, 2021, 04:34:34 pm »
Probably the split you are using. Before in the past I've had to do UBOUND(array)-1. And they can always look at the C++ code to see how it is delimited. I opted for Linux style which is CHR$(10) rather than Windows style which is CHR$(13) + CHR$(10).

Which split are you using? I've had issues before with splitting strings because of the algorithms that are out there. I haven't found one I really like just yet.
« Last Edit: February 06, 2021, 04:38:41 pm by SpriggsySpriggs »
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Question about using SHELL "dir" on non windows OS.
« Reply #48 on: February 06, 2021, 05:09:02 pm »
Probably the split you are using. Before in the past I've had to do UBOUND(array)-1. And they can always look at the C++ code to see how it is delimited. I opted for Linux style which is CHR$(10) rather than Windows style which is CHR$(13) + CHR$(10).

Which split are you using? I've had issues before with splitting strings because of the algorithms that are out there. I haven't found one I really like just yet.

Quote
I opted for Linux style which is CHR$(10) rather than Windows style which is CHR$(13) + CHR$(10).
Oh is this built into the .h file? I approve! not that it matters :)
I consider the 2 byte delimiter the old DOS way and Chr$(10) the newer way, only 1 byte is more efficient.

Quote
Before in the past I've had to do UBOUND(array)-1
Don't you think this indicates an empty string on the end, I am wondering about a Chr$(0) on the end since C likes to do that. Anyway there is something useless at that Ubound.

Split1000 found here:
https://www.qb64.org/forum/index.php?topic=1607.msg108201#msg108201
I am getting perfect counts in my tests of a simple array displayer and my arrays coming out of Split have some empty string character at Ubound of array. Maybe if I trimmed the returned string from pipecom? maybe I should just check that next, before the splitting. ;-))

It handles 0 and 1 base ReDim 'd arrays (dynamic that you can change the size of) it probably could handle an exotic
Code: QB64: [Select]
  1. ReDim(-5 to -5) as string
but I have enough things to test and play with.

I will show you the code when I am done messing with it. I am really interested in the ideal way to do files and folders cross platform. But I suspect ideal depends on the application as usual. So my app is get a filename or pathname from anywhere on hard drive. After that selecting multiple files from different folders for some operation like copy to SD drive maybe for my backups.
« Last Edit: February 06, 2021, 05:10:44 pm by bplus »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Question about using SHELL "dir" on non windows OS.
« Reply #49 on: February 06, 2021, 05:13:24 pm »
Don't you think this indicates an empty string on the end, I am wondering about a Chr$(0) on the end since C likes to do that. Anyway there is something useless at that Ubound.

I take it back.... I remember that sometimes (depending on the command) you might have an extra CHR$(10). You can use LEFT$ or MID$ and just take it off.
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Question about using SHELL "dir" on non windows OS.
« Reply #50 on: February 07, 2021, 02:02:35 am »
Yes at the end of the string returned by pipecom is a delimiter, to be ignored.

I got a crude browser working using pipecom to get a dir listing to navigate with:
Code: QB64: [Select]
  1. _Title "Compare Pipecom to SHELL methods of file data access" 'b+ 2021-02-6
  2. Screen _NewImage(1024, 400, 32)
  3. _Delay .25
  4.  
  5. Declare Library "pipecom"
  6.     Function pipecom$ (cmd As String)
  7.  
  8. 'Dim F As String                 'tests with command line dir calls
  9. ' NOTE: these slants work \    these do not /
  10. 'F = pipecom("dir " + Chr$(34) + "C:\users\marka\desktop\QB64 work\000 work QB64" + Chr$(34) + " /b /A:D") ' Linux needs other switches
  11. 'F = pipecom("dir /b *.BAS") 'if you need just the filenames and not the rest of the output
  12. 'F = pipecom("dir /b /A:D") ' /b = bare list /A:D directories only
  13. 'F = pipecom("dir /n /o:gend") ' long list files on right? well <dir> for dirs and files  ahhhh!
  14.  
  15. ReDim As String choice
  16. While _KeyDown(27) = 0 'until escape
  17.     choice = GetFileName$
  18.     If choice <> "" Then Print: Print "You selected " + choice Else Print: Print "You canceled"
  19.     Print: Print "          press any for another test, escape to quit..."
  20.     Sleep
  21.  
  22. Function GetFileName$ ()
  23.     Dim As String d, s, dr
  24.     Getfolder:
  25.     d = pipecom("dir /n /o:gend") '/n files on right (40) /o = order g for group dirs first, e extension, name, date
  26.     d = Left$(d, Len(d) - 1) ' always ends with delimiter
  27.     ReDim dir$(1 To 1)
  28.     Split d, Chr$(10), dir$()
  29.     s$ = GetArrayItem$(dir$())
  30.     Cls
  31.     If InStr(s$, "<DIR>") Then 'isolate name
  32.         dr$ = _Trim$(Mid$(s$, InStr(s$, "<DIR>") + 6))
  33.         ChDir dr$
  34.         GoTo Getfolder
  35.     ElseIf InStr(s$, "AM") Or InStr(s$, "PM") Then
  36.         GetFileName$ = _Trim$(Mid$(s$, 40))
  37.     End If
  38.  
  39. Sub Split (SplitMeString As String, delim As String, loadMeArray() As String)
  40.     Dim curpos As Long, arrpos As Long, LD As Long, dpos As Long 'fix use the Lbound the array already has
  41.     curpos = 1: arrpos = LBound(loadMeArray): LD = Len(delim)
  42.     dpos = InStr(curpos, SplitMeString, delim)
  43.     Do Until dpos = 0
  44.         loadMeArray(arrpos) = Mid$(SplitMeString, curpos, dpos - curpos)
  45.         arrpos = arrpos + 1
  46.         If arrpos > UBound(loadMeArray) Then ReDim _Preserve loadMeArray(LBound(loadMeArray) To UBound(loadMeArray) + 1000) As String
  47.         curpos = dpos + LD
  48.         dpos = InStr(curpos, SplitMeString, delim)
  49.     Loop
  50.     loadMeArray(arrpos) = Mid$(SplitMeString, curpos)
  51.     ReDim _Preserve loadMeArray(LBound(loadMeArray) To arrpos) As String 'get the ubound correct
  52.  
  53. Function GetArrayItem$ (arr() As String)
  54.     Dim As Long lb, ub, top, i, row, prevrow, n, ad, r
  55.     Dim iNum$, k$
  56.     lb = LBound(arr): ub = UBound(arr): ad = _AutoDisplay
  57.     If ub - lb + 1 < 21 Then top = ub Else top = lb + 19
  58.     Do
  59.         Cls
  60.         Print "       Scroll, Enter number choice, delete to Clear number, or escape to Cancel"
  61.         If ub - lb + 1 > 20 Then
  62.             While _MouseInput
  63.                 row = row + _MouseWheel
  64.                 If row < lb Then row = lb 'prevent under scrolling
  65.                 If row > ub - 19 Then row = ub - 19 'prevent over scrolling
  66.                 If prevrow <> row Then 'look for a change in row value
  67.                     prevrow = row 'store previous row valu
  68.                 End If
  69.             Wend
  70.             r = 2
  71.             For n = row To row + 19
  72.                 Locate r, 1: Print "#"; _Trim$(Str$(n)); ": "; " "; arr(n)
  73.                 r = r + 1
  74.             Next
  75.         Else
  76.             For i = lb To top
  77.                 Print i; " "; arr(i)
  78.             Next
  79.         End If
  80.         Locate 23, 1: Print iNum$
  81.         k$ = InKey$
  82.         If InStr("0123456789", k$) Then
  83.             iNum$ = iNum$ + k$
  84.         ElseIf iNum$ = "" And k$ = "-" Then
  85.             iNum$ = "-"
  86.         ElseIf k$ = Chr$(8) Then
  87.             If Len(iNum$) Then iNum$ = Left$(iNum$, Len(iNum$) - 1)
  88.         ElseIf Len(k$) = 2 And Right$(k$, 1) = Chr$(83) Then
  89.             iNum$ = ""
  90.         ElseIf k$ = Chr$(13) And Len(iNum$) Then
  91.             If Val(iNum$) >= lb And Val(iNum$) <= ub Then GetArrayItem$ = arr(Val(iNum$)): Exit Do Else iNum$ = ""
  92.         ElseIf k$ = Chr$(27) Then
  93.             Exit Function
  94.         End If
  95.         _Display
  96.         _Limit 30
  97.     Loop
  98.     If ad Then _AutoDisplay ' reverse changes
  99.     _KeyClear
  100.