PDA

View Full Version : [SOLVED:] Deleting values / lines from a single text file code changing to deleting from multi



cblake843
10-09-2015, 05:26 AM
I've got the following code which allows me to delete lines from a text file based on a input value i.e. a date 001:2003 etc etc The code works fine for one file at a time , but how could I modify it so as it looks at all files in a directory and modifies them accordingly?



Dim inz As String
Dim outz As String
Dim remove As String
Dim remove2 As String


inz = Me.txtXLFIle
outz = Me.txtXLFIle
remove = Me.Text7
'remove2 = Me.Text9


Const ForReading = 1
Const ForWriting = 2


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(inz, ForReading)


Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If InStr(strLine, remove) = 0 Then
strNewContents = strNewContents & strLine & vbCrLf

End If
Loop


objFile.Close


Set objFile = objFSO.OpenTextFile(outz, ForWriting)
objFile.Write strNewContents


objFile.Close




End Sub

jonh
10-09-2015, 07:24 AM
To get all files in a folder you can use the dir function.
Pass the variable inz into your sub from another sub.

e.g.


Sub test()

Const sPath As String = "c:\"
Dim sFile As String

sFile = Dir(sPath) '<-- SET START FOLDER
Do Until sFile = ""
DoSomethingWith sPath & sFile
sFile = Dir '<-- GET NEXT FILE
Loop


End Sub


Sub DoSomethingWith(inz As String)
'Dim inz As String
Dim outz As String
Dim remove As String
Dim remove2 As String
'...ect
End Sub

cblake843
10-11-2015, 11:25 PM
Thanks for the reply but I can't get it to work. No files are found, perhaps I misunderstood but how to reference the directory finding sub in the second sub where file editing takes place?

jonh
10-12-2015, 03:09 AM
how to reference the directory finding sub in the second sub where file editing takes place?

Why would you want to?


inz is the file you are processing.

Sub test() goes through a folder and passes the file names to sub DoSomethingWith


Sub DoSomethingWith(inz As String)

That's why 'Dim inz As String' is commented out because it is already dimensioned.

You need to remove this line from DoSomethingWith:


inz = Me.txtXLFIle

and in test() replace


Const sPath As String = "c:\"

with something like


Dim sPath As String

sPath = Me.txtXLFolder

cblake843
10-12-2015, 03:41 AM
Private Sub Command0_Click()

Const sPath1 As String = "H:\DesktopW7\PARDET AR23538\TCS\OLD\"


Dim sFile1 As String



Dim outz As String
Dim remove As String
'Dim remove2 As String


'inz = Me.txtXLFIle
'outz = Me.txtXLFIle
remove = Me.Text7






sFile1 = Dir(sPath1) '<-- SET START FOLDER

Do Until sFile1 = ""
'MsgBox sPath & sFile


Const ForReading = 1
Const ForWriting = 2


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(sPath1 & sFile1, ForReading)


Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If InStr(strLine, remove) = 0 Then
strNewContents = strNewContents & strLine & vbCrLf

End If


Loop



objFile.Close


Set objFile = objFSO.OpenTextFile(sPath1 & sFile1, ForWriting)
objFile.Write strNewContents


objFile.Close



sFile1 = Dir '<-- GET NEXT FILE
Loop


Thanks again for the reply. I've partially got it working now but what happens is the code now appends the last file read to current file being edited for example.


File 1 = 485 Kb
File 2 = 485 kb
file 3 = 485 kb

after the code has completed this has happened to files and they have been appended with data from other files (which I don't want to happen)

file1 = 485 KB
file 2 = 970 KB
file 3 = 1455 KB

Can you see where I'm going wrong in the above code ?




















End Sub

jonh
10-12-2015, 04:06 AM
Please use code tags.

You need to clear strNewContents before writing the next file.

cblake843
10-12-2015, 04:13 AM
Genius, that did it. Many thanks.