I would like to be able to open all the files in a folder, and sub folders (based on wildcard), then go between each of the open files, and delete all the contents of the open files, and save them AUTOMATICALLY. These are log files (text format), and I want cleared out periodically (the programs don't give that option). THANKS IN ADVANCE.
Or if you want to keep the files, simply overwrite each file with nothing.
It is quite easy to open all files in a directory using a wildcard or a directory using a list file. UltraEdit supports this. It is also easy to start UltraEdit with the wildcard or list file and a script to execute which deletes the content of each opened file and saves it. But such a solution would be much slower than simply deleting all files or overwriting each file with nothing directly from a batch file.
Here is an example for a batch file which overwrites (= truncates to 0 bytes) all *.log files in directory C:\Temp\Logs and subdirectories.
- Code: Select all
cd /D C:\Temp\Logs\
dir *.log /B /S >"%TEMP%\FileList.tmp"
for /F "usebackq delims=" %%F in ("%TEMP%\FileList.tmp") do echo. 2>"%%F"
del "%TEMP%\FileList.tmp" >nul
Used information from How to create empty text file from a batch file?
It would be also possible to run the FOR command directly on the list of files in a directory without getting the list of files first into a temporarily created text file. But I noticed in the past that the FOR command has problems when the number of files matching the wildcard pattern changes in the directory while FOR command processes the files. Therefore it is more safe to get first the names of the files to process into a text file and process this list definitely not changing while FOR is processing the files from this list file.