By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,594 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,594 IT Pros & Developers. It's quick & easy.

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

100+
P: 133
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
Share this Question
Share on Google+
5 Replies


kadghar
Expert 100+
P: 1,295
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

100+
P: 133
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
Expert 100+
P: 1,295
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

100+
P: 133
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
Expert 100+
P: 1,295
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

Post your reply

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