Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File System Object Question 2

Status
Not open for further replies.

AlexCuse

Programmer
Apr 13, 2006
5,416
US
Hi all,

I have a routine that renames several .dat files received from a client and also counts the lines in each one. However, I find myself needing to open in word pad and do a save as, presumably because there is a need to replace vbCr with vbCrLf. I found a vbscript method of fixing this at this thread - thread329-1240224

I am trying to convert this to access, but cannot find the command to tell filesystemobject to save. Here is the applicable code:

Code:
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)          'Specify Folder
Set fc = f.Files

for each f1 in fc
'only applicable code included

           Set ots = f1.OpenAsTextStream(1)
'is it a problem with my OpenAsTextStream constant?
           s2 = Replace(ots.readall, vbCr, vbCrLf)
'what to put here to save?  first guess was ots.save       
           ots.Close

Any advice would be greatly appreciated.

Thanks,

Alex


Professor: But what about your superintelligence?
Gunther: When I had that there was too much pressure to use it. All I want out of life is to be a monkey of moderate intelligence who wears a suit. That's why I've decided to transfer to Business School.
Professor: NOOOOOOOOOOOO.
 
In Access it would be something like
Code:
Dim fs                          As FileSystemObject
Dim ots                         As TextStream
Dim f1                          As File
Dim S2                          As String
Const FolderSpec                As String = "C:\myFolder"

Set fs = New FileSystemObject

For Each f1 In fs.GetFolder(FolderSpec).Files

    S2 = Replace(f1.OpenAsTextStream(ForReading).ReadAll, vbCr, vbCrLf)

    Set ots = fs.OpenTextFile(f1.Name, ForWriting, True)
    ots.Write S2
    ots.Close

Next
 
...
Set ots = f1.OpenAsTextStream(1) '1=ForReading
s2 = Replace(ots.ReadAll, vbCr, vbCrLf)
ots.Close
Set ots = f1.OpenAsTextStream(2) '2=ForWriting
ots.Write s2
ots.Close
...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Golom - do I need to have any special references to use the OpenTextFile method your example uses? It is not running for me (access 2k3)

PH- your suggestion runs, but does not give the desired result.

Perhaps I am wrong about the delimiting character? Is VbCr the correct constant to use for a square character delimiting the lines in a .dat file?

Thanks in advance,

Alex


Professor: But what about your superintelligence?
Gunther: When I had that there was too much pressure to use it. All I want out of life is to be a monkey of moderate intelligence who wears a suit. That's why I've decided to transfer to Business School.
Professor: NOOOOOOOOOOOO.
 
Perhaps this ?
s2 = Replace(ots.ReadAll, vbLf, vbCrLf)

And for safety:
s2 = Replace(Replace(ots.ReadAll, vbCr, ""), vbLf, vbCrLf)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
You should have a reference to "Microsoft Scripting Runtime" that will expose the FileSystem Object, the Textstream Object, and the File object.

The "square" character is the default for many non-displayable characters. ".dat" is a pretty general extension and is used by a lot of different systems to contain almost anything so there is no "standard" line delimiter used in all ".dat" files.

Try changing "vbCr" to "vbLf" and see what happens.

You might also replace "ForReading" with "1" and "ForWriting" with "2" as PHV has done.
 
Golom - Microsoft Scripting Runtime was not activated. Suppose this was why I had to use late binding for Set fs = CreateObject("Scripting.FileSystemObject")? Good to know in the future.

I stuck with PH's method (the change to Lf, CrLf worked like a charm) because this would not require another reference being added.

Thanks for all your help guys. Stars all around.

Alex


Professor: But what about your superintelligence?
Gunther: When I had that there was too much pressure to use it. All I want out of life is to be a monkey of moderate intelligence who wears a suit. That's why I've decided to transfer to Business School.
Professor: NOOOOOOOOOOOO.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top