Solved

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

Posted on 2013-11-05
7
677 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now