Tapatalk

Clear content of all log files in a directory tree

Clear content of all log files in a directory tree

18
Basic UserBasic User
18

    Sep 03, 2013#1

    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.

    6,688587
    Grand MasterGrand Master
    6,688587

      Sep 03, 2013#2

      Why do you not simply delete all those files instead of deleting their contents?

      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

      @echo off
      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.

      18
      Basic UserBasic User
      18

        Mar 17, 2014#3

        I completely forgot to reply to this. The reason is that the files are required by the program, and the program doesn't create the files, once the user deletes them. The only way to "recreate" them is to reinstall the program, so I suspect the programmer doesn't know how to create text files. Since the original post, I've abandoned the program. But thanks your help anyway.