Solved

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

Posted on 2013-11-05
7
671 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

13 Experts available now in Live!

Get 1:1 Help Now