Как проверить, существует ли каталог в qbasic?
Я пишу программу на 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 ответов
Попробуйте изменить каталог на blablabla
, Если он не существует, будет ошибка. Перехватите эту ошибку и укажите процедуру обработки ошибок.
ON ERROR GOTO doesnotexist
CHDIR "c:\user\basic\blablabla\"
END
doesnotexist:
MKDIR "c:\user\basic\blablabla\"
CHDIR "c:\user\basic\blablabla\"
RESUME NEXT
Если память служит (а иногда нет):
FolderExists = (Dir$("C:\User\basic\blahblahbla\nul") <> "")
должен работать в более старых версиях BASIC, которые поддерживают Dir$(), но не поддерживают параметр attribute. Предполагается, что устройство NUL существует в каждой папке, поэтому это способ проверки папки, даже если эта папка пуста.
Другой способ обнаружить каталог существует в QB:
REM function to detect directory exists in QB pd 2019 ejo
REM load QB /L QB.QLB
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)
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
PRINT "Enter directory";
INPUT Filespec$
ASCIIZ = Filespec$ + CHR$(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
IF (OutregsX.flags AND &H1) = &H0 THEN
' store filename attribute bits
Filebits% = ASC(DTAfile.Filebits)
' check directory bit
IF (Filebits% AND &H10) = &H10 THEN
PRINT "Directory exists."
ELSE
PRINT "Filename exists."
END IF
ELSE
PRINT "Filespec not found."
END IF
' restore current dta
InregsX.AX = &H1A00
InregsX.DS = Current.DTA.SEG
InregsX.DX = Current.DTA.OFF
CALL InterruptX(&H21, InregsX, OutregsX)
END
Еще одна подпрограмма для подсчета каталогов / имен файлов в 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
Еще один пример для обнаружения каталога существует в QB64:
PRINT "Enter dirspec";: INPUT Spec$
IF _DIREXISTS(Spec$) THEN
PRINT "Directory exists."
ELSE
PRINT "Directory not found."
END IF
END