Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Writing a Text File


Reply
 
Thread Tools Display Modes
  #1  
Old 04-03-2002, 10:19 AM
sasa sasa is offline
Centurion
 
Join Date: Jun 2001
Location: CA
Posts: 150
Post Writing a Text File


Hi all, is there a way to write all of the files within a directory onto a text file using vb code? please help

ss
Reply With Quote
  #2  
Old 04-03-2002, 10:34 AM
Bucky's Avatar
Bucky Bucky is offline
A+ Certified Nerd

Retired Leader
* Expert *
 
Join Date: Dec 2001
Location: East Coast
Posts: 1,763
Default

Do the filenames have consistant titles, like "File0001.txt", "File0002.txt", etc? Then you could just loop and check if the file exists. If it does, then open the file and write to it.
__________________
If you're not part of the solution, you're part of the precipitate.
Reply With Quote
  #3  
Old 04-03-2002, 10:51 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,928
Default

Use the Dir function to get all files in a directory (see the help for an example). Use the VB file routines (open, line input, print) to read and write the files.
Reply With Quote
  #4  
Old 04-03-2002, 11:28 AM
loquin's Avatar
loquin loquin is offline
Google Hound

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Arizona, USA
Posts: 12,386
Default

If the object is to write the contents of the files into a single file, then do as per ArnoutV; if the object is to save the file names only, then instead of opening the files, reading & copying them line by line, just write the file name to the text file.
__________________
Lou
"I have my standards. They may be low, but I have them!" ~ Bette Middler
"It's a book about a Spanish guy called Manual. You should read it." ~ Dilbert
"To understand recursion, you must first understand recursion." ~ unknown
Reply With Quote
  #5  
Old 04-03-2002, 12:14 PM
Squirm's Avatar
Squirm Squirm is offline
Political Coder

Retired Moderator
* Guru *
 
Join Date: Mar 2001
Location: London, England
Posts: 8,037
Default

The Dir$() function can be a hard nut to crack. You start by calling it with all the parameters, to basically 'tell' it what you want it to retrieve. On all subsequent calls, you leave out the parameters and it simply repeats whatever action you did last. Your code might end up something like this:

Code:
Dim sFile As String     'Filename string variable
Dim sPath As String     'Directory string variable

sPath = "C:\"

sFile = Dir$(sPath, vbNormal)     'Look for the first file
Do Until sFile = ""
    'Do stuff with the sFile here, such as:
    Open sPath & sFile For Input As #1
        'Read stuff from file
    Close #1
    
    Debug.Print sPath & sFile  'Print the filename to the debug window
    
    sFile = Dir$()  'Grab next file
Loop    'Repeat until no more files remain                             
__________________
Search the forums | Use [vb][/vb] tags | Still IRCing
Reply With Quote
  #6  
Old 04-03-2002, 03:15 PM
sasa sasa is offline
Centurion
 
Join Date: Jun 2001
Location: CA
Posts: 150
Default

Thank you very much everyone!!!!
Reply With Quote
  #7  
Old 04-03-2002, 07:36 PM
BillSoo's Avatar
BillSoo BillSoo is offline
Code Meister

Retired Moderator
* Guru *
 
Join Date: Aug 2000
Location: Vancouver, BC, Canada
Posts: 10,441
Default

You could also cheat and just shell a batch file....

Shell "SaveDir.Bat"

And then write the batch file like:

Dir *.* > dir.txt
__________________
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
Reply With Quote
  #8  
Old 04-03-2002, 09:09 PM
BlueCube BlueCube is offline
Centurion
 
Join Date: Apr 2002
Location: Saratoga NY
Posts: 105
Default

first go to references and make sure "microsoft scripting runtime" is selected

then add this to the general setion:

Dim fsys As New FileSystemObject

code:

dim FileNum as integer
dim thisfolder as folder
dim thisfile as file
dim thesefiles as files
set thisfolder = fsys.getfolder(<folderpath>)
set thesefiles = thisfolder.files
FileNum = FreeFile
open <Filename> for ouput as #FileNum
for each thisfile in thesefiles
write #FileNum, thisfile.name
next
close #FileNum
__________________
-- Nick

"oooo! they have the internet on the computer now!!" - Homer Simpson
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->