Solved

Need VBS to delete and recreate all TXT & CSV files in a folder

Posted on 2013-11-05
7
685 Views
Last Modified: 2013-11-05
Hi,

I am pretty new to VB script.  This is probably easy, but quite not sure how to proceed.  I am using TXT and CSV files to produce a monthly report.  My last step is to  empty TXT and CSV files in order to be ready for next month. Empty files must be present to start next month process.

I was thinking of a VB Script that would do the following :
     For all TXT and CSV files in a specific network folder:
         read the filename and extension
         delete the file
         recreate the file as per the cached filename and extension

Does it make sense?

Your suggestions are welcome
Thanks for your help
0
Comment
Question by:mldaigle1
  • 3
  • 3
7 Comments
 
LVL 8

Expert Comment

by:virtuadept
ID: 39624841
When you say recreate the file, are you saying you just want a 0 byte file with the same file name on the folder instead of the one that has data in it?  Is this some requirement of the software responsible for populating the files?
0
 
LVL 4

Expert Comment

by:slimpckins28
ID: 39624863
i think the opening all txt files in a folder and deleting contents would work too, correct?
0
 

Author Comment

by:mldaigle1
ID: 39624955
@virtuadept

exact recreate the file at 0 bytes.  This is effectively a kind of requirement for populate date in the files
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 8

Expert Comment

by:virtuadept
ID: 39624958
I am not sure the why of this but here is VBscript code to do what you asked to do, replace the "." with the path you want to do this on:

  Dim fcnt
  Dim objFileSys
  Dim objFolder1
  Dim objFile1
  Dim Fname
  
  Set objFileSys = CreateObject("Scripting.FileSystemObject")
  Set objFolder1 = objFileSys.GetFolder(".")
  For Each objFile1 in objFolder1.files
	Fname = Ucase(Trim(objFile1.Name))
	' debug WScript.Echo "Name " + Fname  + " Type " + Fname 
  	If Right(Fname,3) = "TXT" or Right(Fname,3) = "CSV" Then
		objFileSys.DeleteFile objFile1, TRUE
		objFileSys.CreateTextFile Fname, TRUE, FALSE
	End If
  Next

Open in new window

0
 

Author Comment

by:mldaigle1
ID: 39624959
@slimpckins28

This would work too for me as long as I find all the TXT and CSV files in the folder at 0 bytes
0
 
LVL 8

Accepted Solution

by:
virtuadept earned 250 total points
ID: 39624977
And I just realized my code would change the case of your file names which may not be desired so you can do this instead:

  Dim fcnt
  Dim objFileSys
  Dim objFolder1
  Dim objFile1
  Dim Fname
  Dim FnameOrig
  
  Set objFileSys = CreateObject("Scripting.FileSystemObject")
  Set objFolder1 = objFileSys.GetFolder(".")
  For Each objFile1 in objFolder1.files
	Fname = Ucase(Trim(objFile1.Name))
        FnameOrig = objFile1.Name
	' debug WScript.Echo "Name " + Fname  + " Type " + Fname 
  	If Right(Fname,3) = "TXT" or Right(Fname,3) = "CSV" Then
		objFileSys.DeleteFile objFile1, TRUE
		objFileSys.CreateTextFile FnameOrig, TRUE, FALSE
	End If
  Next

Open in new window

0
 

Author Closing Comment

by:mldaigle1
ID: 39625264
Thanks, this is working great!! :)
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question