Link to home
Start Free TrialLog in
Avatar of bfuchs
bfuchsFlag for United States of America

asked on

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
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

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?
Avatar of bfuchs

ASKER

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
what about deleting the file and then creating a 0 byte file with the same name?
SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bfuchs

ASKER

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
Avatar of Bill Prew
Bill Prew

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
Avatar of bfuchs

ASKER

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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bfuchs

ASKER

Thank you my experts!