Support > General Discussions

Having trouble getting execute command to work

<< < (2/2)

Support:
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

timm:
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: ---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()
--- End code ---

I can make run just by commenting out line 13

--- Code: ---  a = execute("cmd /c attrib " + dname + " > tempattrib.txt" , -1, pid)

--- End code ---
I had been erroring on line 23 when it was written like this:

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

--- End code ---
but then I changed it to

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

--- End code ---
and it started working

Support:
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
[*]SbCollectDirectories Collect the directory names as well as file names into the file list.
[*]SbCollectDots Collect the virtual . and .. directory names into the list.
[*]SbCollectRecursively Collect the files from the directory and from all the directories below.
[*] SbCollectFullPath The list will contain the full path to the file names. This means that the file names returned by the function NextFile will contain the directory path specified in the open directory statement and therefore can be used as argument to file handling commands and functions.
[*]SbCollectFiles Collect the files. This is the default behavior.
[*]SbSortBySize The files will be sorted by file size.
[*]SbSortByCreateTime The files will be sorted by creation time.
[*]SbSortByAccessTime The files will be sorted by access time.
[*] SbSortByModifyTime The files will be sorted by modify time.
[*] SbSortByName The files will be sorted by name. The name used for sorting will be the bare file name without any path even if the option bit SbCollectPath is specified.
[*] SbSortByPath The files will be sorted by name including the path. The path is the relative to the directory, which is currently opened. This sorting option is different from the value sbSortByName only when the value sbCollectRecursively is also used.
[*] SbSortAscending Sort the file names in ascending order. This is the default behavior.
[*] SbSortDescending Sort the file names in descending order.
[*] SbSortByNone Do not sort. Specify this value if you do not need sorting. In this case directory opening can be much faster especially for large directories.
[/list]

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

Hope this helps.


--- Code: ---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

--- End code ---


John

Navigation

[0] Message Index

[*] Previous page

Go to full version