Code to delete contents of all files

Hi Experts,
I'm looking for either code or script that will do the following
loop thru all files of specific folder, not including sub folders (folder should only contain CSV files).
delete all the contents of those files, but do not delete the file itself.
Thanks
LVL 6
bfuchsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongSoftware Team LeadCommented:
delete all the contents of those files, but do not delete the file itself.

are those csv files contain the headers? do you want to remove or retain the headers when you "emptying" the file content?

what if just deleting the files and recreate them with new files with empty content?
bfuchsAuthor Commented:
Hi,
Need to remove headers as well.
what if just deleting the files and recreate them with new files with empty content?
That may work, as long the name of files are exactly the same.
See screenshot.
Thanks,
Ben
Untitled.png
David Johnson, CD, MVPRetiredCommented:
what about deleting the file and then creating a 0 byte file with the same name?
Maximize Customer Retention with Superior Service

The IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more to help build customer satisfaction and retention.

Ryan ChongSoftware Team LeadCommented:
think process can be simplified since you're writing to CSV files:

test.vbs

Const ForWriting = 2

sFolder = "D:\yourPath\ftp\"
Set oFSO = CreateObject("Scripting.FileSystemObject")

sfileList = ""

For Each oFile In oFSO.GetFolder(sFolder).Files
  If UCase(oFSO.GetExtensionName(oFile.Name)) = "CSV" Then
     Set File = oFSO.OpenTextFile(oFile.path,  ForWriting)
     File.Close
     Set File = Nothing
  End if
Next

Open in new window

bfuchsAuthor Commented:
Hi Experts,
what about deleting the file and then creating a 0 byte file with the same name?
As mentioned, this should be fine.
@Ryan,
Will test it tom at work.
Thanks,
Ben
Bill PrewIT / Software Engineering ConsultantCommented:
You should be able to do this command right from a DOS command prompt to empty the files, just adjust the folder name as needed.

for %A in ("c:\temp\*.csv") do @echo.>"%~A"

Open in new window

And if you truly want a 0 byte file (rather than an empty text file) then you could do:

for %A in ("c:\temp\*.csv") do @copy /y NUL "%A">NUL

Open in new window


»bp
bfuchsAuthor Commented:
Hi Experts,

Tested and both work, great!

Now one question if you dont mind...
Since I constantly need to check for date/time they were created.
and with the utility above its something difficult (see attached).
would it be possible to
a- retain the file originally created date
b- display that in explorer, instead of date modified.

Thanks,
Ben
Untitled.png
Bill PrewIT / Software Engineering ConsultantCommented:
a- retain the file originally created date

No easy way to do this with the DOS command approach.

Should be doable with VBS, try the modified code below.

b- display that in explorer, instead of date modified.

You can configure the columns that are displayed in File Explorer by right clicking on the header columns and selecting the various properties.

Const ForWriting = 2

sFolder = "D:\yourPath\ftp\"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("Shell.Application")

Set oFolder = oShell.NameSpace(sFolder)

For Each oFile In oFSO.GetFolder(sFolder).Files
  If UCase(oFSO.GetExtensionName(oFile.Name)) = "CSV" Then
     sFile = oFile.Name
     sOldDate = oFile.DateLastModified
     With oFSO.OpenTextFile(oFile.path,  ForWriting)
        .Close
     End With
     oFolder.Items.Item(sFile).ModifyDate = sOldDate
  End if
Next

Open in new window


»bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bfuchsAuthor Commented:
Thank you my experts!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.