.TXT files deletion from local folder using VBA Macros "KILL"

maleodillet

New Member
Joined
Feb 15, 2018
Messages
12
Hi Gurus,


I am using VBA to generarte a .TXT file that gets picked up by a SAP process chain. The Code works well, the problem I have is that I have to be able to delete the file once I am done running the process chain but I am being unsuccessful.


So far I'm just trying to put together the routine that I would later insert into my main code but is is not working for me. What I am trying to achieve is to loop the routine to delete all the .TXT files sitting in the folder path, the code as follows;




Public Sub KillPromtFile()


Dim TxtFile As String
Dim FileFolder As String
Dim PathFolderl As String


TxtFile = Environ$("USERPROFILE") & "\Desktop\*.TXT"
FileFolder = Dir$(Environ$("USERPROFILE") & "\Desktop\*.TXT")
PathFolder = Environ$("USERPROFILE") & "\Desktop"


Do While FileFolder <> ""
Kill PathFl & FileFolder
FileFolder = Dir$(Environ$("USERPROFILE") & "\Desktop\*.TXT")
Loop
End Sub


I am not a VBA ninja so please excuse the extra declared variables...any help would be deeply appreciate it. Thanks in Advance:confused:
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try
Code:
Sub KillPromtFile()
   Dim Fname As String
   Dim Pth As String
   
   Pth = Environ("userprofile") & "\Desktop\"
   Fname = Dir(Pth & "*.txt")
   Do While Fname <> ""
      Kill Pth & Fname
      Fname = Dir()
   Loop
End Sub
 
Upvote 0
Also, the Kill statement accepts wildcards. So if the intent is to delete all .txt files at the end of your process, you can delete them all at once...

Code:
Kill [COLOR=#333333]Environ("userprofile") & "\Desktop\*.txt"[/COLOR]

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,215,467
Messages
6,124,984
Members
449,201
Latest member
Lunzwe73

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top