Solved

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

Posted on 2013-11-05
7
705 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
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 …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Suggested Courses

617 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