Solved

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

Posted on 2013-11-05
7
687 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
OID and Active Directory Attributes 9 71
Problem to open text file 11 131
Batch file or script with password 22 69
get the ALL CAPITAL words from a cell 4 20
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 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