Code to delete contents of all files

bfuchs
bfuchs used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ryan ChongSoftware Team Lead

Commented:
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?
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
Top Expert 2016

Commented:
what about deleting the file and then creating a 0 byte file with the same name?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Ryan ChongSoftware Team Lead
Commented:
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

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 Consultant
Top Expert 2016

Commented:
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
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
IT / Software Engineering Consultant
Top Expert 2016
Commented:
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
Thank you my experts!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial