Everything about Files & Folders (File System Object in VBA) - Create, Delete, Read, Move, Creation Date and Many More

 Sub test()

'MkDir "D:\Test\"

'MkDir "D:\Rust\"

'FileCopy "D:\Test\txt.txt", "D:\Test\txt-2.txt"

'Kill "D:\Test\txt-2.txt"

Name "D:\Test\txt.txt" As "D:\Rust\txt.txt"

End Sub


Sub Movefile()

Dim fso As Scripting.FileSystemObject

Set fso = New Scripting.FileSystemObject

fso.Movefile "D:\Rust\txt.txt", "D:\Test\txt.txt"

End Sub


Create Text File and Read Data in Excel Line by Line.

Sub CreateTextFile()

Dim fso As Scripting.FileSystemObject

Set fso = New Scripting.FileSystemObject

Dim txt As Scripting.TextStream

Dim i As Integer

i = 1

Set txt = fso.CreateTextFile("D:\Test\txt-3.txt", True)

txt.WriteLine "This is first line"

txt.WriteBlankLines (1)

txt.WriteLine "This is second line"

'Set txt = fso.CreateTextFile("D:\Text\txt-4.txt", True)

Set txt = fso.OpenTextFile("D:\Test\txt-3.txt")

Do Until txt.AtEndOfStream

Cells(i, 1) = txt.ReadLine

i = i + 1

Loop

txt.Close

Set fso = Nothing

End Sub


Create Text File and Read All Data in one go.

Sub Createtxt()
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
Dim txt As Scripting.TextStream
Set txt = fso.CreateTextFile("D:\Test\MakeTxt.txt")
txt.Write "This is first line. Followed by second line"
Set txt = fso.OpenTextFile("D:\Test\MakeTxt.txt")
Sheet1.Range("a1") = txt.ReadAll
End Sub








Comments

Popular posts from this blog

Power Automate - Automatically fetch data from Power BI in to Excel and Send the copy of the Excel file via Email

Separate Text (Characters) & Numbers from Alpha Numeric String Using Formula and Macro (VBA)

File System Object