ScriptBasic

Support => General Discussions => Topic started by: timm on September 17, 2008, 07:56:21 PM

Title: Having trouble getting execute command to work
Post by: timm on September 17, 2008, 07:56:21 PM
When I attempt to use the execute command I get the following error "(0): error &H1:Not enough memory"
For example the following code will cause the error.

Code: [Select]
a = execute ("dir /b > list.txt", -1, pid)


Any suggestions would be appreciated, this is a real sumbling block for me.
Title: Re: Having trouble getting execute command to work
Post by: timm on September 17, 2008, 08:06:29 PM
almost as soon as I posted this I discovered the solution, the following code works fine.

Code: [Select]
a = execute ("cmd /c dir /b > list.txt", -1, pid)
any way hopefully I will save some one some frustration.
Title: Re: Having trouble getting execute command to work
Post by: Support on September 18, 2008, 06:32:34 AM
Thanks Tim for the report on how you solved your problem.

Posting issues/solutions here helps others traveling the same road.


John
Title: Re: Having trouble getting execute command to work
Post by: timm on September 19, 2008, 11:37:03 PM
Ok, my problem isn't solved after all. I guess I found a poor work around, not a solution. The following code does produce the (0): error &H1:Not enough memory error.
  a = execute("cmd /c attrib newtest.sb > tempattrib.txt" , -1, pid)

the only solution I can think of at the moment, would be to write the commands out as batch files and execute them that way, but I really don't want to, its just so cludgy
Interestingly the command does execute in isolation, so it must be using just a LITTLE to much memory the program that it executes in is less than 40 lines of code and doesn't do anything extreme with strings or arrays.

This is driving me nutz
Title: Re: Having trouble getting execute command to work
Post by: Support on September 20, 2008, 10:33:40 AM
Hi Timm,

I remember having an issue with redirects with execute under Linux where it would output to the screen instead of a file. I mentioned it to Peter and ended up using a shell script that called scriba and did the redirect stuff in a script.

I will give it a try under Windows and see if I can reproduce your problem. There may be a issue with execute and shell redirection and should be reported in the bug tracker.

John
Title: Re: Having trouble getting execute command to work
Post by: Support on September 20, 2008, 11:24:19 AM
Timm,

I just tried your execute command on my Windows XP box and it worked fine.

Note: only line in program.

a = execute ("cmd /c dir /b > list.txt", -1, pid)

The list.txt file contained the directory contents (filenames only)

What version of ScriptBasic are you using?

My test was with the 2.1 version.


John
Title: Re: Having trouble getting execute command to work
Post by: timm on September 20, 2008, 03:02:01 PM
I am using ver. 2.1  compiled on 2/16/05
I can get it to execute in isolation, but can't get it to work in anything more elaborate than "hello world"
Here is the specific code, its really not much
Code: [Select]
function grabFhandle
  if isundef(lastHandle) or lastHandle >= 512 then
    lastHandle = 1
  else
    lastHandle += 1
  end if
  grabFhandle = lastHandle
end function

function fileAttribute(dname)
  local fHandle
  fHandle  = grabFhandle()
  a = execute("cmd /c attrib " + dname + " > tempattrib.txt" , -1, pid)
  open "tempattrib.txt" for input as fHandle
  line input #fHandle, oneline
  close #fHandle
  fileAttribute = left(oneline, 5)
end function

function test
  local fHandle
  fHandle  = grabFhandle()
  a = execute ("cmd /c dir /b > list.txt", -1, pid)
  open "list.txt" for input as fHandle
  x = 0
  line input #fHandle, oneDir
  do
    dirArray[x, 1] = oneDir
dirArray[x, 2] = fileAttribute(oneDir)
print dirArray[x, 2], "\n"
    x = x + 1
    line input #fHandle, oneDir
  loop until eof(1)
  close #fHandle
  print "lbound = ", lbound(dirArray), " ubound = ", ubound(dirArray), "\n"
  for x=lbound(dirArray[]) to ubound(dirArray[])
    print dirArray[x,1]
  next
end function

dummy = test()

I can make run just by commenting out line 13
Code: [Select]
  a = execute("cmd /c attrib " + dname + " > tempattrib.txt" , -1, pid)
I had been erroring on line 23 when it was written like this:
Code: [Select]
  a = execute ("dir /b > list.txt", -1, pid)
but then I changed it to
Code: [Select]
  a = execute ("cmd /c dir /b > list.txt", -1, pid)
and it started working
Title: Re: Having trouble getting execute command to work
Post by: Support on September 20, 2008, 05:34:10 PM
Timm,

You can use FREEFILE() to return the next available file handle.

fhandle = FREEFILE()

You can just close the file when your done and reuse the file handle again if you want.

I think your making this harder then it needs to be.

Check out the ScriptBasic wiki for directory open & close and the nextfile command to get the next file. (optionally based on a pattern)

http://www.scriptbasic.org/wiki/index.php?title=ScriptBasic:UsersGuide_12.14

This way you don't need the execute() at all.

Here is a bit of code I snagged from my MLS utility that removes old photos of properties that have gone off market. My directory structure is made up of 1000 sub-directories numbered from 000 to 999. The last three digits of the MLS # determines which sub-directory the photo(s) resides in. The following code would read all 1000 directories selecting only .jpg files and print the file names.

OPTIONS

Note: Use AND if more then one option is needed with the OPEN DIRECTORY directive.

Hope this helps.

Code: [Select]
fn = FREEFILE

FOR D = 0 TO 999
  DName = FORMAT("%~000~",D)
  OPEN DIRECTORY DName PATTERN "*.jpg" OPTION sbCollectFiles AS #fn
  RESET DIRECTORY #fn
  GOSUB READ_DIR
  CLOSE DIRECTORY #fn
NEXT D

END

READ_DIR:

FName = NEXTFILE(FN)
IF FName = undef THEN RETURN
PRINT Fname & "\n"
GOTO READ_DIR


John