Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Reading and Writing files

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,49167
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Reading and Writing files
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Reading and Writing files - 7/8/2007 1:28:30 PM   
  peachtea

 

Posts: 41
Score: 0
Joined: 7/5/2007
From: Singapore
Status: offline
Hi All

I am new to VS script and this is the 1st script i need to write.

I've got 2 dat files which needed to be read and written onto a new dat file in a folder

I've written something but it doesn't seems to do anything, though there's no compilation error.
Is there any kind soul who is willing to guide me along?



Here is my code:

      

So far, i've visited Hey! Scripting guy, http://msdn2.microsoft.com/en-us/library/czxefwt8.aspx and http://www.tutorial-web.com/asp/fso/textstream.asp?property=Write for the production of the codes that i've posted above.

thanks for your help!
 
 
Post #: 1
 
 RE: Reading and Writing files - 7/8/2007 2:25:38 PM   
  dm_4ever


Posts: 2724
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
First off, you overwrite the value of strLine and strLine1 so you end up with the last line of each of the text files

Do until objFile.AtEndOfStream
       strLine = objFile.Readline
Loop

should be more like.


Do until objFile.AtEndOfStream
        strLine = strLine  & objFile.Readline
Loop

or to just get the entire content

strLine = objFile.ReadAll
objFile.Close


Is this all your code?

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to peachtea)
 
 
Post #: 2
 
 RE: Reading and Writing files - 7/8/2007 2:35:30 PM   
  peachtea

 

Posts: 41
Score: 0
Joined: 7/5/2007
From: Singapore
Status: offline
yup that is all for my code.

I just need to read from the 2 files and write them onto the 3rd file (you can call it merging the data also)

i've use the readAll method but there is still no file being produced....

btw, is this the correct way of writing a read-and-write files script?


(in reply to dm_4ever)
 
 
Post #: 3
 
 RE: Reading and Writing files - 7/8/2007 2:40:12 PM   
  dm_4ever


Posts: 2724
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
Well if that is all you have then the reason nothing is getting written is because everything is in its own Sub which is a good thing as long as you pass the values around (preferred) or you would need to define global variables. You can find discussions about those two topics here since it has been discussed several times.  Anyhow...the following may work.


      

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to peachtea)
 
 
Post #: 4
 
 RE: Reading and Writing files - 7/8/2007 8:25:42 PM   
  peachtea

 

Posts: 41
Score: 0
Joined: 7/5/2007
From: Singapore
Status: offline
Wow amazing

Just another qn, from the few discussions that i've search thru the forum, I suppose that the reason you have declare the variables as global ones is because both my input files are fixed and it is needed to be access by the rest of the sub in the same program right?

What if my input filenames can be dynamic? (but directory path is fixed)

Do I have to separate them into different sub?

Something like the script will be run periodically 1minute apart to look for new dat files in the respective folder and after that, will merge into the 3rd dat files in the d:\merging\working folder.

I was thinking of renaming the output files using timestamps like this:

      

Please advice.
Thanks again :)



(in reply to dm_4ever)
 
 
Post #: 5
 
 RE: Reading and Writing files - 7/9/2007 1:47:38 PM   
  dm_4ever


Posts: 2724
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
Even if you use Subs, global variables are not needed.

i.e.

      

Again, forgive me but I won't go into details on the topic of subs/function and global/local variables because it has been discussed several times.
i.e. http://www.visualbasicscript.com/m_45440/tm.htm

You can define the names of the files you are reading and writing dynamically however you see fit.  I would suggest you play with the current example and read the WSH Docs to understand how and why everything is currently working so that you can change it to your needs.

Good luck and let us know if you get stuck somewhere...just post what you have at that time along with the error and line generating the error.

< Message edited by dm_4ever -- 7/9/2007 2:06:28 PM >


_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to peachtea)
 
 
Post #: 6
 
 RE: Reading and Writing files - 7/9/2007 8:54:24 PM   
  peachtea

 

Posts: 41
Score: 0
Joined: 7/5/2007
From: Singapore
Status: offline
ok! I've confirmed that the input filenames will not be dynamic.

Now im asked to rename the input filenames before reading them and delete them once read (hv tested my code and it works)
However, it only works if both my input files have characters in it and if the 2nd input file in empty.

I'm asked to read both files and if either one is empty, it will still read and just write it on the output file. (if one file is empty then it will just write what it read in the non-empty file and write it into the output file... oops i hope u understand what i mean )

ok... now I don't know how to get the file to read and write even if the 1st file is empty and there's character in 2nd file.
Pls help~!

This is the error msg:
Line: 39
char: 4
Error: input past end of file
Code: 800A003E
Source: Microsoft VBScript runtime Error

The output file result is empty.

My code:

      

  Please guide me how to rectify this. Thanks alot once again

(in reply to dm_4ever)
 
 
Post #: 7
 
 RE: Reading and Writing files - 7/10/2007 1:16:06 AM   
  dm_4ever


Posts: 2724
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
A quick fix would simply be to put On Error Resume Next (turn off error msg) and then On Error Goto 0 for the section you know you will get errors and in this case it is acceptable and expected from time to time.
...code
           On Error Resume Next
            objMergedFile.WriteLine objFSO.OpenTextFile("D:\Merging\GL\pl-GLFile.dat", ForReading).ReadAll
            objMergedFile.WriteLine objFSO.OpenTextFile("D:\Merging\AS400\pl-AS400File.dat", ForReading).ReadAll
            On Error GoTo 0
...code


      

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to peachtea)
 
 
Post #: 8
 
 RE: Reading and Writing files - 7/10/2007 12:04:15 PM   
  peachtea

 

Posts: 41
Score: 0
Joined: 7/5/2007
From: Singapore
Status: offline
hey... I did as what you've advised... wow amazing... now even if the 1st file is empty, it is able to read n write

now i can move on to the next stage on my script

Thanks alot.... this is so much fun than Java

(in reply to dm_4ever)
 
 
Post #: 9
 
 RE: Reading and Writing files - 7/11/2007 1:09:06 AM   
  ehvbs

 

Posts: 2223
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
I strongly object to:

    A quick fix would simply be to put On Error Resume Next (turn off error msg) and then On Error Goto 0
    for the section you know you will get errors and in this case it is acceptable and expected from
   time to time.

The sample code disables Error handling for approximately 14 different and risky operations. If one of
them (e.g. the first writing to the output file) fails for any reason (file locked by another process) you'll
get wrong results and no indication of the problem whatsoever. It would be much better to avoid the
OERN completely and use a function like

Function ReadAllFromFile( sFSpec )
Dim sRVal : sRVal = ""
With goFS.OpenTextFile( sFSpec, ForReading )
   If Not .AtEndOfStream Then sRVal = .ReadAll
   .Close
End With
ReadAllFromFile = sRVal
End Function

to get an empty string from an empty file resp. the content from decent ones.

(in reply to peachtea)
 
 
Post #: 10
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Reading and Writing files Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts