如何测试目录是否存在于qbasic中?

时间:2019-01-09 09:44:38

标签: directory exists qbasic gw-basic

我正在用Qbasic编写程序。 我想知道如何测试文件夹是否存在。

想法是:

IF "c:\user\basic\blablabla\" exists (?? how to programm the "exist" test?)
THEN CHDIR "c:\user\basic\blablabla\"
ELSE 
MKDIR "c:\user\basic\blablabla\"
CHDIR "c:\user\basic\blablabla\"
ENDIF

我希望我足够清楚

非常感谢您的建议!

:)

5 个答案:

答案 0 :(得分:5)

尝试将目录更改为blablabla。如果不存在,将出现错误。捕获此错误并指定错误处理例程。

ON ERROR GOTO doesnotexist
CHDIR "c:\user\basic\blablabla\"
END

doesnotexist:
MKDIR "c:\user\basic\blablabla\"
CHDIR "c:\user\basic\blablabla\"
RESUME NEXT

答案 1 :(得分:2)

如果内存可用(有时不可用):

FolderExists = (Dir$("C:\User\basic\blahblahbla\nul") <> "")

应该在支持Dir $()但不支持attribute参数的BASIC的较早版本中工作。 NUL设备假定存在于每个文件夹中,因此即使该文件夹为空,它也是一种检查文件夹的方法。

答案 2 :(得分:1)

QB中存在另一种检测目录的方法:

response.d

答案 3 :(得分:1)

另一个在QB45中计算目录/文件名的子例程:

DECLARE SUB CheckSpec (Var$, Var1!, Var2!)
REM subroutine to count directories\filenames in QB pd 2019 ejo
REM load QB /L QB.QLB
REM links qb.lib into qb.qlb
REM   link /q qb.lib,qb.qlb,Nul,bqlb45.lib;
TYPE DTAtype
  Drive AS STRING * 1
  SearchTemplate AS STRING * 11
  SearchAttr AS STRING * 1
  EntryCount AS STRING * 2
  ClusterNumber AS STRING * 2
  Reserved AS STRING * 4
  Filebits AS STRING * 1
  FileTime AS STRING * 2
  FileDate AS STRING * 2
  FileSize AS STRING * 4
  ASCIIZfilename AS STRING * 13
END TYPE
TYPE RegTypeX
 AX    AS INTEGER
 BX    AS INTEGER
 CX    AS INTEGER
 DX    AS INTEGER
 bp    AS INTEGER
 si    AS INTEGER
 di    AS INTEGER
 flags AS INTEGER
 DS    AS INTEGER
 ES    AS INTEGER
END TYPE
DECLARE SUB InterruptX (intnum AS INTEGER, inreg AS RegTypeX, outreg AS RegTypeX)
DO
  COLOR 15, 0
  PRINT "Enter filespec(*.*)";
  INPUT Filespec$
  IF Filespec$ = "" THEN Filespec$ = "*.*"
  COLOR 14, 0
  PRINT "Searching: "; Filespec$
  CALL CheckSpec(Filespec$, Var1, Var2)
  COLOR 15, 0
  IF Var1 THEN PRINT "Directories:"; Var1
  IF Var2 THEN PRINT "Filenames:"; Var2
  IF Var1 = 0 AND Var2 = 0 THEN PRINT "No files foound."
  COLOR 14, 0
  PRINT "Again(y/n)?";
  LOCATE , , 1
  DO
     x$ = INKEY$
     IF LCASE$(x$) = "n" THEN PRINT : COLOR 7, 0: END
     IF LCASE$(x$) = "y" THEN PRINT : COLOR 7, 0: EXIT DO
  LOOP
LOOP
END
' var1=dirs, var2=files
SUB CheckSpec (Var$, Var1, Var2)
 DIM InregsX AS RegTypeX
 DIM OutregsX AS RegTypeX
 DIM DTAfile AS DTAtype
 DIM ASCIIZ AS STRING * 260
 DIM Current.DTA.SEG AS INTEGER
 DIM Current.DTA.OFF AS INTEGER
 ASCIIZ = UCASE$(Var$) + CHR$(0)
 Var1 = 0: Var2 = 0
 ' store current dta
 InregsX.AX = &H2F00
 CALL InterruptX(&H21, InregsX, OutregsX)
 Current.DTA.SEG = OutregsX.ES
 Current.DTA.OFF = OutregsX.BX
 ' store function dta
 InregsX.AX = &H1A00
 InregsX.DS = VARSEG(DTAfile)
 InregsX.DX = VARPTR(DTAfile)
 CALL InterruptX(&H21, InregsX, OutregsX)
 ' findfirst
 InregsX.AX = &H4E00
 InregsX.CX = &H37
 InregsX.DS = VARSEG(ASCIIZ)
 InregsX.DX = VARPTR(ASCIIZ)
 CALL InterruptX(&H21, InregsX, OutregsX)
 ' check carry flag error
 DO
    IF (OutregsX.flags AND &H1) = &H0 THEN
       ' store filename attribute bits
       Filebits% = ASC(DTAfile.Filebits)
       ' check directory bit
       IF (Filebits% AND &H10) = &H10 THEN
          Var1 = Var1 + 1
       ELSE
          Var2 = Var2 + 1
       END IF
       ' find next filename
       InregsX.AX = &H4F00
       CALL InterruptX(&H21, InregsX, OutregsX)
    ELSE
       EXIT DO
    END IF
 LOOP
 ' restore current dta
 InregsX.AX = &H1A00
 InregsX.DS = Current.DTA.SEG
 InregsX.DX = Current.DTA.OFF
 CALL InterruptX(&H21, InregsX, OutregsX)
END SUB

答案 4 :(得分:0)

QB64中存在另一个用于检测目录的示例:

PRINT "Enter dirspec";: INPUT Spec$
IF _DIREXISTS(Spec$) THEN
    PRINT "Directory exists."
ELSE
    PRINT "Directory not found."
END IF
END