1.

Solve : How to Read File using VBScript?

Answer»

I'm new to VBScript. I've found a lot of DOCUMENTATION, but can't seem to accomplish my first task.

I simply want to Read each line of a text file and be able to do something with that line.

My code is attached.

I seemed to try a gazillion things that don't work.
When I try execute OpenAsTextStream, I get an Invalid Call or Arguement message.

The rest of the code is just a guess at what I need to do next.

[size=14]Thanx in advance for your help![/size][/color]
Code: [Select]ReadAll
msgbox "Done!"

sub ReadAll()
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TriStateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
dim fso
dim s
dim f
dim ts
set fso = createobject("Scripting.FileSystemObject")
set f=fso.GetFile("H:\AppDevCtr\DistAppl\DataFile.Txt")
set ts=f.OpenAsTextStream(ForReadng, TriStateUseDefault)

'------------------------------------------------------------------------
'--- Iterate through the entire file
'------------------------------------------------------------------------
'
' This is where I wing it because I really have no idea of what to do!
'
'------------------------------------------------------------------------
Do While not f.eof and not f.bof
s = ts.ReadLine
msgbox s, vbOkOnly, "This is a line of text!"
' Do I have to do anything here to go to the next line
loop
end subI made a few changes but you can SEE the general idea.

Code: [Select] Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TriStateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

set fso = createobject("Scripting.FileSystemObject")
set ts=fso.OpenTextFile("H:\AppDevCtr\DistAppl\DataFile.Txt", ForReading)

'------------------------------------------------------------------------
'--- Iterate through the entire file
'------------------------------------------------------------------------
'
' This is where I wing it because I really have no idea of what to do!
'
'------------------------------------------------------------------------
Do While not ts.AtEndOfStream
s = ts.ReadLine()
msgbox s, vbOkOnly, "This is a line of text!"
Loop
ts.Close

Hope this helps. 8-)

Note: you really don't need all those CONSTs but no harm, no foul. There is a compiled help file for VBScript and JScript. Try searching your machine for Script56.CHM.Oh my, Sidewinder! You've been a tremendous help all weekend. One last question, and I'm done (for today anyway!)

The last thing I need to do is use the values I got from my external file and placed in SArray - and use them to do an XCOPY.
(I know FSO has a file copying FEATURE, but I'm in a rush and I need to use some of XCOPYs parameters - or learn how to code for them tonite - and I just don't have time.)
What do I have to do to execute the XCOPY command?

[size=12]Thanx [/size]- [size=14]AGAIN[/size]


Code: [Select]ReadAll
MsgBox "done"


Sub ReadAll
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TriStateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim SArray
set fso = createobject("Scripting.FileSystemObject")
set ts=fso.OpenTextFile("H:\AppDevCtr\DistAppl\DataFile.Txt", ForReading)

'------------------------------------------------------------------------
'--- Iterate through the entire file
'------------------------------------------------------------------------
Do While not ts.AtEndOfStream
s = ts.ReadLine()
SArray = Split(S, " ", 2, vbTextCompare)

msgbox trim(sarray(0)) & " - " & trim(sarray(1)), vbOkOnly, "XCOPY first file to second file"
'----------------------------------------------------------------------
' I need XCOPY to be executed here using the SArray Parameters
'----------------------------------------------------------------------
'xcopy SArray(0) SArray(1)
Loop
End subYou can create the Shell object and then use the Run method.

Code: [Select]ReadAll
MsgBox "done"


Sub ReadAll
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TriStateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim SArray
set fso = createobject("Scripting.FileSystemObject")
set ts=fso.OpenTextFile("H:\AppDevCtr\DistAppl\DataFile.Txt", ForReading)
Set WshShell = CreateObject("WScript.Shell")

'------------------------------------------------------------------------
'--- Iterate through the entire file
'------------------------------------------------------------------------
Do While not ts.AtEndOfStream
s = ts.ReadLine()
SArray = Split(S, " ", , vbTextCompare)

msgbox trim(sarray(0)) & " - " & trim(sarray(1)), vbOkOnly, "XCOPY first file to second file"
'----------------------------------------------------------------------
' I need XCOPY to be executed here using the SArray Parameters
'----------------------------------------------------------------------
cmd = "XCopy " & SArray(0) & " " & SArray(1)
WshShell.Run cmd,,True
Loop
ts.Close
End sub

If you need switches for the XCOPY, add them as literals when building the cmd statement. The True on the Run statement will wait for each XCOPY to complete before the loop continues. This is important as each XCOPY is started as a new process.

Note: I removed the "2" from the Split statement. Better to let the interpreter decide how many substrings are returned.

Good luck. 8-)You're a gentleman and a scholar! I really appreciate the help - especially on a weekend!

[size=14]Thank you[/size] and [size=12][size=14]Good Night[/size][/size]And just when I thought I was done - I have to ask my very first question all over again - but for VBScript - not .BAT

How do I stop XCOPY from asking me if the destination is a File or a Directory.
In the .BAT file, I echoed an F in front of the XCOPY command. In VBScript, that does not seem to work.

[size=14]Thanx again!!!![/size]

Code: [Select]Readall
MsgBox "done"


Sub ReadAll
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TriStateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim SArray

set fso = createobject("Scripting.FileSystemObject")
set ts=fso.OpenTextFile("H:\AppDevCtr\DistAppl\DataFile.Txt", ForReading)
Set WshShell = CreateObject("WScript.Shell")

'------------------------------------------------------------------------
'--- Iterate through the entire file
'------------------------------------------------------------------------
Do While not ts.AtEndOfStream
s = ts.ReadLine()
SArray = Split(S, " ", 2, vbTextCompare)

'----------------------------------------------------------------------
' I need to programatically answer F to the XCOPY prompt
'----------------------------------------------------------------------
cmd = "XCopy " & Trim(SArray(0)) & " " & Trim(SArray(1)) & "/y /q "
' cmd = "echo f | XCopy " & Trim(SArray(0)) & " " & Trim(SArray(1)) & "/y /q /k /d"
MsgBox cmd,,"cmd"
WshShell.Run cmd,,True
Loop
End subUsing the command shell from a VBScript seems to be defeating the whole purpose, especially when the FileSystemObject has so much functionality. But hey! it's your script

Code: [Select]Readall
MsgBox "done"


Sub ReadAll
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TriStateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim SArray

set fso = createobject("Scripting.FileSystemObject")
set ts=fso.OpenTextFile("H:\AppDevCtr\DistAppl\DataFile.Txt", ForReading)
Set WshShell = CreateObject("WScript.Shell")

'------------------------------------------------------------------------
'--- Iterate through the entire file
'------------------------------------------------------------------------
Do While not ts.AtEndOfStream
s = ts.ReadLine()
SArray = Split(S, " ", 2, vbTextCompare)

'----------------------------------------------------------------------
' I need to programatically answer F to the XCOPY prompt
'----------------------------------------------------------------------
cmd = WshShell.ExpandEnvironmentStrings("%comspec%") & " /c echo f | XCopy " & Trim(SArray(0)) & " " & Trim(SArray(1)) & " /y /q /k /d"
MsgBox cmd,,"cmd"
WshShell.Run cmd,,True
Loop
End sub

Note 1: I took the liberty of adding a leading space before the /y switch.

Note 2: Keep in mind that creating objects within a subroutine limits their scope.

Question: I thought the XCOPY /d switch was for date selection. Am I wrong?

8-)They don't call me NEWBIE for nothing.

My objective is to copy a list of files (or directory) from one location to another - with one two nuances

    [*]First - only copy files which are newer in the source directory than the correstponding file in the destination directory. This is accomplished by using the /d parameter without a date.[/list]
      [*]Second - only copy files which currently exist in the destination directory. This is accomplished by the /u parameter. [/list]
      If you could show me how to accomplish this using VBScript - I would be absolutely ESTATIC. This is a learning experience for me - and you've shown me a lot.
      I'm a creature of habit and I'm familiar with XCOPY. But -I'd much rather make the MOVE to FSO as it is more appropriate in this application.

      I have not been able to find or download script56.chm - probably because I am working on a Remote Desktop Connection. I'm gonna try later on another computer. Maybe then, I won't have to bug you with so many quesetions.

      Thanx!


      You not bugging us. We're more than happy to help.

      This may need some fine tuning as I don't have a clear picture what's in Datafile.txt

      Code: [Select] Const ForReading = 1

      Set WshShell = CreateObject("WScript.Shell")
      Set fso = CreateObject("Scripting.FileSystemObject")
      Set ts=fso.OpenTextFile("H:\AppDevCtr\DistAppl\DataFile.Txt", ForReading)

      Do While not ts.AtEndOfStream
      s = ts.ReadLine()
      SArray = Split(S, " ", 2, vbTextCompare)
      Set filein = fso.GetFile(Trim(SArray(0)))
      Set fileout = fso.GetFile(Trim(SArray(1)))

      If fso.FileExists(fileout.ParentFolder & "\" & filein.Name) Then 'source in dest?
      If filein.DateCreated > fileout.DateCreated Then 'source newer than dest?
      fso.CopyFile filein, fileout, True
      End If
      End If
      Loop
      ts.Close
      MsgBox "Done"

      The script was checked for syntax errors not for logic errors. 8-)Fine tuning - No way! It worked like a charm as soon as I corrected my data file.

      In the code you created for me you have the following line:
      Code: [Select]Set fileout = fso.GetFile(Trim(SArray(1)))How would I set Fileout to the desktop or a folder on the desktop?

      Thanx!The DESKTOP is one of many Special Folders in Windows. They're pretty much all handled with some boilerplate code.

      Code: [Select]Const ForReading = 1
      Const DESKTOP = &H10&

      Set WshShell = CreateObject("WScript.Shell")

      Set objShell = CreateObject("Shell.Application")
      Set objFolder = objShell.Namespace(DESKTOP)
      Set objFolderItem = objFolder.Self

      Set fso = CreateObject("Scripting.FileSystemObject")
      Set ts=fso.OpenTextFile("H:\AppDevCtr\DistAppl\DataFile.Txt", ForReading)

      Do While not ts.AtEndOfStream
      s = ts.ReadLine()
      SArray = Split(S, " ", 2, vbTextCompare)
      Set filein = fso.GetFile(Trim(SArray(0)))
      fileout = objFolderItem.Path 'concatenate additonal folders if needed

      If fso.FileExists(fileout.ParentFolder & "\" & filein.Name) Then 'source in dest?
      If filein.DateCreated > fileout.DateCreated Then 'source newer than dest?
      fso.CopyFile filein, fileout, True
      End If
      End If
      Loop
      ts.Close
      MsgBox "Done"

      It should be no problem to concatenate any additional folders to the desktop path.

      Good luck. 8-)


      Discussion

      No Comment Found