473,394 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How do i open a text file as input that may be in two different locations?

133 100+
Hello,
I am trying to use a text file as input but it could be in two locations.
for example :

Open "c:\mytext.txt" For Input As #1
While Not EOF(1)
Line Input #1, temp$
alltext$ = alltext$ & temp$ & vbCrLf
Wend
Label1.Caption = alltext$
Close #1

but the text file might also be in c:\program files\mytext\mytext.txt

___VB6___

Any help will be much appreciated.
Thankyou Gobblegob.
Dec 8 '07 #1
5 1501
kadghar
1,295 Expert 1GB
Hello,
I am trying to use a text file as input but it could be in two locations.
for example :
...
Any help will be much appreciated.
Thankyou Gobblegob.
use a string,
then just change it, you can use an input box, for example:

Expand|Select|Wrap|Line Numbers
  1. dim Str1 as string
  2. str1=inputbox("write here the path and filename")
  3. Open Str1 For Input As #1
HTH
Dec 8 '07 #2
gobblegob
133 100+
use a string,
then just change it, you can use an input box, for example:

Expand|Select|Wrap|Line Numbers
  1. dim Str1 as string
  2. str1=inputbox("write here the path and filename")
  3. Open Str1 For Input As #1
HTH

Thanks for your help kadghar,

But is there a way of looking for the file in the other possible location without user input?

Thanks Gobblegob.
Dec 8 '07 #3
kadghar
1,295 Expert 1GB
Thanks for your help kadghar,

But is there a way of looking for the file in the other possible location without user input?

Thanks Gobblegob.
yeap, there are many ways, the fastest will be with an error handler, so if you try to open a file that doesnt exist, it'll cause an error and try to open the other one.

Expand|Select|Wrap|Line Numbers
  1. on error goto NotHere
  2. str1 = "c:\firstlocation.txt"
  3. open str1 '...
  4. '...
  5. exit sub
  6. NotHere:
  7. on error goto 0 'in case you dont find the 2nd file you dont enter a loop
  8. str1="c:\secondlocation.txt"
  9. open str1 '...
  10. '...
  11. end sub
but if you want it to be a better code, i recomend you to use a DIR to check if the file exists before trying to open it
HTH
Dec 8 '07 #4
gobblegob
133 100+
yeap, there are many ways, the fastest will be with an error handler, so if you try to open a file that doesnt exist, it'll cause an error and try to open the other one.

Expand|Select|Wrap|Line Numbers
  1. on error goto NotHere
  2. str1 = "c:\firstlocation.txt"
  3. open str1 '...
  4. '...
  5. exit sub
  6. NotHere:
  7. on error goto 0 'in case you dont find the 2nd file you dont enter a loop
  8. str1="c:\secondlocation.txt"
  9. open str1 '...
  10. '...
  11. end sub
but if you want it to be a better code, i recomend you to use a DIR to check if the file exists before trying to open it
HTH

Thankyou So Much Kadghar,
I got it to work and and i learned about error handling :)

Thanks again mate.
Gobblegob.
Dec 9 '07 #5
kadghar
1,295 Expert 1GB
Thankyou So Much Kadghar,
I got it to work and and i learned about error handling :)

Thanks again mate.
Gobblegob.
Anytime,
But as i told you, this is some kind of "lazy coding". Searching the file first will be the "correct" way to do it.
Dec 10 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Andras Gilicz | last post by:
Hi VB fans I'm working on a relatively large project in VB6 with about a dozen forms, including graphs, labels, text boxes, etc. The software itself is actually a flow simulator with more or...
1
by: lawrence | last post by:
This PHP function prints out a bunch of Javascript (as you can see). This is all part of the open source weblog software of PDS (www.publicdomainsoftware.org). We had this javascript stuff...
4
by: Dan | last post by:
Can anyone offer suggestions on how to do this or if it is possible? I have a form that uses a drop down box and 2 text fields. What I am trying to do is have the value of each text box set by...
1
by: dwainew | last post by:
Problem: I'd like to archive off the contents of a text file. However, other processes may have the file open, so MoveTo(newfilepath) can't be done, as far as I've found. Start with a 5 MB...
8
by: someone | last post by:
I'm making an program that encodes text, and then writes it into another text file. The problem is that I can't seem to get the "file exists" checking right. If I use the command (coder is the...
2
by: DragonShade | last post by:
To be honest, I'm not sure if this is a .NET issue, some configuration on a PC, or a setting in a database but here goes: Environment (have NO control over): XP (London & Tokyo),.NET 2.0 C#, 2...
5
by: BD | last post by:
I am coding with C# in Visual Studio 2005 for a database application residing on remote MS SQL Server 2005. What I want to do is open the same form but from 2 different places and only one...
9
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
I want to open a text file and format it into a specific line and then apply color to a specific location of the text and then display it in a RichTextBox after all of this is done. I can do all...
1
by: gobblegob | last post by:
Hello, I am trying to use a text file as input but it could be in two locations. for example : Open "c:\mytext.txt" For Input As #1 While Not EOF(1) Line Input #1, temp$ ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.