Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GET TXT FILES IN FOLDER

Status
Not open for further replies.

THEMAN101

Programmer
Apr 29, 2003
44
US
I WANT TO SEE IF THERE ARE ANY TEXT FILES IN A CERTAIN FOLDER. i WANT TO KNOW HOW MANY AND THEN I WANT TO DISPLAY THEIR NAMES, AND THEN I WANT TO DISPLAY THE FILE ITSELF.

THANKS
 
It is rude to use all upper caps in your message. It is considered as if you are screaming/angry at someone
 
>>It is rude to use all upper caps in your message. It is considered as if you are screaming/angry at someone

*ONLY IF YOU TAKE IT THAT WAY ;-)

I HATE that concept...

As far as I am concerned... A is a and B is b no matter how you look at it...

When programming... Espescially with QB... It is usually easier to leave capslock on... and easy to forget to turn it off...

if you want to SCREAM something...
DO IT LIKE THIS

Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
thanks qube that IS what happned. Oh shoot i poot is in caps, i better go turn my self in to the cops.

 
Personally, I don't think a suggestion to shell to a DIR command (with or without a multitude of switches) is a practical solution. I base this note on the assumption that THEMAN101 is interested in retrieving information about certain files and using it inside his program, rather than just displaying a list of files inside a shell.

I have noted the following thread several times recently but I guess it bears repeating: Thread314-198826. You will find ways to get the names, sizes, attributes, creation dates and creation times of whatever files you want to deal with... and use that information within your program.

With a bit of work you could create a file manager that allows you to display the files in a directory... even click on them to open or run them.

As far as posting with CAPS ON.... I always saw it as a cry for help rather than a way of yelling at everybody. Just don't be known as the "boy who cried wolf." That's a bad rap that is hard to live down (pardon the slang, please).


Real men don't use Interrupt 21h.
 
Do it like this: to display the text files in the particular folder use the FILES statement like this:
FILES c:\folder\*.txt

In order to display the file you will have to open it like an INPUT file and print it page for page on the screen

OPEN a:\folder\file.txt FOR INPUT AS #1
DO
FOR q = 1 to 15
INPUT #1, a$
PRINT a$
NEXT q
WHILE INKEY$ = "":WEND
LOOP UNTIL LOF(1)
CLOSE
 
QBKing,
There are a few things wrong w/ your code...

OPEN "a:\folder\file.txt" FOR INPUT AS #1
DO
'You might want to add this here if you want to display 15 files at a time...
LOCATE 1

FOR q = 1 to 15
INPUT #1, a$
PRINT a$
IF EOF(1) THEN EXIT FOR
NEXT
WHILE INKEY$ = "":WEND
LOOP UNTIL EOF(1)
CLOSE


Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
Here... this is based loosely on some code I posted elsewhere:
[tt]
DEFINT A-Z
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
TYPE DTAinfo
Reserved AS STRING * 21
Attribute AS STRING * 1
FileTime AS INTEGER
FileDate AS INTEGER
FileSize AS LONG
FileName AS STRING * 13
END TYPE
DIM inRegs AS RegTypeX, Outregs AS RegTypeX
DIM DTA AS DTAinfo

CLS
'Let's look for all text files in the current directory....

SearchFile$ = "*.TXT" + CHR$(0)
'All of the important information is stored in the
'Disk Transfer Area (DTA). We could call Interrupt 21h
'function 2Fh to find the memory segment and offset of the DTA, then
'use the PEEK statement in a loop to scan memory to retrieve
'the information. But let's get a little sneaky....
'Let's create a UDT and tell the OS to package and deliver all
'of the DTA information in a predefined data type so we won't have to mess
'with any cumbersome PEEKs. We can just get the file size (for example) by
'doing something like: PRINT "The size of the file is " ; DTA.FileSize

inRegs.AX = &H1A00
'Set the DTA to the address of the user defined type....

inRegs.DS = VARSEG(DTA)
inRegs.DX = VARPTR(DTA)
CALL INTERRUPTX(&H21, inRegs, Outregs)
'That was pretty painless.... We'll just have to remember that QB likes to
'move its variables around from time to time. If QB decides to change the
'address of the DTA, it certainly won't bother to tell DOS about the new
'location. The OS might overwite the current contents of the address that was
'set by interrupt 21h, function 1Ah and QB could crash with a
'"corrupt string space" error, or worse :).
'My tests of this code didn't create a problem like this...
'but if you experience a "corrupt string space" error or enounter
'a blue screen under Win NT, you might try refreshing Interrupt 21h,
'function 1Ah periodically while looping through the files.

'Now for some action....
'Find first matching file. (Find the first match for "*.TXT")

inRegs.DS = VARSEG(SearchFile$)
inRegs.DX = SADD(SearchFile$)
inRegs.CX = &HFF
inRegs.AX = &H4E00
CALL INTERRUPTX(&H21, inRegs, Outregs)
IF Outregs.Flags AND 1 THEN
PRINT SearchFile$; " was not found."
'If the file doesn't exist, quit.

SYSTEM
END IF
Cnt = 1
'Call a subroutine to print out a bunch of information about the file
'and then print the actual contents of the file....

GOSUB PrintInfo
'Loop through all of the text files in the current directory
'using the FindNextMatchingFile function (Int21h, funct. 4Fh).

DO
inRegs.AX = &H4F00
CALL INTERRUPTX(&H21, inRegs, Outregs)
IF Outregs.Flags AND 1 THEN
'Found all of the text files, so quit

SYSTEM
END IF
Cnt = Cnt + 1
GOSUB PrintInfo
LOOP
SYSTEM

PrintInfo:
'The file name is stored in the 13 bytes of the DTA
'beginning at the 31st position. The name will be padded with
'NULL characters (ASCII 0's).
'Let's print out the file counter and the ASCII 0 padded file name,
'along with some of the other information....

PRINT Cnt; ") File Info: "; DTA.FileName
PRINT "File size: "; DTA.FileSize; " bytes"
'Extract the Year, Month and Day values from the date value stored in the DTA

Yr$ = LTRIM$(STR$(FIX(DTA.FileDate / 512) + 1980))
Mn! = (DTA.FileDate MOD 512) / 32
Mn$ = RIGHT$("00" + LTRIM$(STR$(INT(Mn!))), 2)
Dy! = (Mn! - FIX(Mn!)) * 32
Dy$ = RIGHT$("00" + LTRIM$(STR$(FIX(Dy!))), 2)
PRINT "File creation date: "; Mn$; "-"; Dy$; "-" + Yr$
'Extract the creation time from the DTA and parse it into a meaningful format.

Hours = INT(DTA.FileTime / 2048)
Hours$ = RIGHT$("00" + LTRIM$(STR$(Hours)), 2)
Mins! = ((DTA.FileTime MOD 2048) / 32)
Mins$ = RIGHT$("00" + LTRIM$(STR$(INT(Mins!))), 2)
Secs! = INT((Mins! - INT(Mins!)) * 60)
Secs$ = RIGHT$("00" + LTRIM$(STR$(Secs!)), 2)
PRINT "File creation time: "; Hours$ + ":" + Mins$ + ":" + Secs$
'The last item is the file attribute (Archive, Read Only, Hidden, System, etc.)
'Since it is only one byte long, we convert it to a number by finding its
'ASCII value....

FlAttrib = ASC(DTA.Attribute)
PRINT "File attribute"; FlAttrib
PRINT STRING$(79, "-")
'Now let's print the contents of the text file....

ff = FREEFILE
'Remove the ASCII 0 padding from the file name...

FiName$ = LEFT$(DTA.FileName, INSTR(DTA.FileName, CHR$(0)) - 1)
'Open the file...

OPEN FiName$ FOR BINARY AS #ff
'Create a string long enough to hold the file contents...

FileContent$ = STRING$(LOF(ff), 32)
'Get the file contents...

GET #ff, 1, FileContent$
'Close the file...

CLOSE #ff
'Print it out...

PRINT FileContent$
PRINT STRING$(79, "-")
RETURN
[/tt]
Just how far do you want to go with this? Do you want to place the names and attributes of all text files in an array?





Real men don't use Interrupt 21h.
 
Hmmm.... it occured to me that not everybody had a copy of QB45 or higher to allow them to access the DOS interrupts. So I wrote this little piece of fluff to allow Qbasic v1 users to get the files in a diectory and place them in an array for later use. Basically, all that this does is to fill the screen with the names of the text files in the current directory using the FILES command. Then it places those names in an array by scraping the screen (peeking into video memory and parsing the contents into a list of files).

Obviously, this strategy won't work very well when there are many text files in the current directory, but it should work fairly well when the number of files is limited to one page of information. Anyway... here goes. Try it out.
[tt]
DEFINT A-Z
'Clear the screen

CLS
Ext$ = "TXT"
FILES "*." + Ext$
'Set the default memory segment to the beginning of text screen memory

DEF SEG = &HB800
Scr$ = ""
'"Scrape" a portion of the screen and put it in a string....

FOR Re = 0 TO 1024
p = PEEK(Re)
IF p > 31 THEN Scr$ = Scr$ + CHR$(p)
NEXT
DEF SEG
CLS
'Count the number of text files returned by FILES

FileCnt = 0
Gpos = 1
'Go through the string and find the appropriate file
'extensions bounded by "." and a space....

DO
Gpos = INSTR(Gpos, Scr$, "." + Ext$ + " ")
IF Gpos > 0 THEN
FileCnt = FileCnt + 1
Gpos = Gpos + 4
ELSE
EXIT DO
END IF
LOOP
'Create an array to store the filenames in....

REDIM MyFileArray$(1 TO FileCnt)
'Loop through the string and look for file names, starting with the
'first occurance of twenty spaces (this skips the first entry,
'which is the name of the directory in question)....
'Then fill the array with the file names.

Re = INSTR(Scr$, STRING$(20, 32))
Element = 1
DO
NextTxt = INSTR(Re, Scr$, Ext$)
IF NextTxt > Re THEN
MyFile$ = RTRIM$(LTRIM$(MID$(Scr$, Re, NextTxt - Re + 3)))
MyTmpFile$ = ""
FOR Re = 1 TO LEN(MyFile$)
Tmp$ = MID$(MyFile$, Re, 1)
IF ASC(Tmp$) > 32 THEN MyTmpFile$ = MyTmpFile$ + Tmp$
NEXT
MyFileArray$(Element) = MyTmpFile$
Element = Element + 1
Re = NextTxt + 3
ELSE
EXIT DO
END IF
LOOP
'Finally, print out the file names stored in the array....

FOR Element = 1 TO UBOUND(MyFileArray$)
PRINT MyFileArray$(Element)
NEXT
[/tt]



Real men don't use Interrupt 21h.
 
I've augmented Alt255's code above to squeeze just a few more filenames (if needed).

--MiggyD


DEFINT A-Z
SCREEN 12
'Clear the screen
CLS
Ext$ = "TXT"
WIDTH , 50
FILES "*." + Ext$
SLEEP 10
'Set the default memory segment to the beginning of text screen memory
DEF SEG = &HB800
Scr$ = ""
'"Scrape" a portion of the screen and put it in a string....
FOR Re = 0 TO 3840
p = PEEK(Re)
IF p > 31 THEN Scr$ = Scr$ + CHR$(p)
NEXT
DEF SEG
CLS
'Count the number of text files returned by FILES
FileCnt = 0
Gpos = 1
'Go through the string and find the appropriate file
'extensions bounded by "." and a space....
DO
Gpos = INSTR(Gpos, Scr$, "." + Ext$ + " ")
IF Gpos > 0 THEN
FileCnt = FileCnt + 1
Gpos = Gpos + 4
ELSE
EXIT DO
END IF
LOOP
'Create an array to store the filenames in....
REDIM MyFileArray$(1 TO FileCnt)
'Loop through the string and look for file names, starting with the
'first occurance of twenty spaces (this skips the first entry,
'which is the name of the directory in question)....
'Then fill the array with the file names.
Re = INSTR(Scr$, STRING$(20, 32))
Element = 1
DO
NextTxt = INSTR(Re, Scr$, Ext$)
IF NextTxt > Re THEN
MyFile$ = RTRIM$(LTRIM$(MID$(Scr$, Re, NextTxt - Re + 3)))
MyTmpFile$ = ""
FOR Re = 1 TO LEN(MyFile$)
Tmp$ = MID$(MyFile$, Re, 1)
IF ASC(Tmp$) > 32 THEN MyTmpFile$ = MyTmpFile$ + Tmp$
NEXT
MyFileArray$(Element) = MyTmpFile$
Element = Element + 1
Re = NextTxt + 3
ELSE
EXIT DO
END IF
LOOP
'Finally, print out the file names stored in the array....
CLS
FOR Element = 1 TO UBOUND(MyFileArray$)
PRINT MyFileArray$(Element),
NEXT
LOCATE 50, 1
PRINT "Total " + Ext$ + " Files:" + STR$(Element)
SLEEP 10

--MiggyD
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top