2

I have a folder with logs.

This folder contains a lot of log files. I cannot delete these files because server keeps it. But I can remove content of every file. It is very annoying to open each file and remove content.

Can you provide ideas to make it faster?

phuclv
  • 30,396
  • 15
  • 136
  • 260

4 Answers4

0

I had the same situation as yours, where a log file is continuously written by some app and I have to clear the content without closing the app. Fortunately I have only 1 file and I can open it in Notepad++, press Ctrl+A then press Delete. In case there are multiple files just use the following PowerShell script

Get-ChildItem -Recurse log*.txt | ForEach-Object {
    $fs = [IO.FileStream]::new($_.FullName, `
        [IO.FileMode]::Truncate, [IO.FileAccess]::Write, [IO.FileShare]::Write)
    $fs.Close()
}

(Alternatively call $fs.SetLength(0) to truncate the file if [IO.FileMode] is not Truncate)

This works because the log file was opened in FILE_SHARE_WRITE mode, contrary to many people's beliefs that you can't delete or edit a file in Windows while it's opened. Subsequent opens must also be in shared mode. That's why > or >> in cmd or PowerShell doesn't work, because they open the file normally. Here's a very simple C++ to demonstrate how to create a file in shared mode

#define WIN32_LEAN_AND_MEAN 
#include <windows.h>
#include <fileapi.h>
#include <cstdlib>
#include <ctime>
#include <cstdio>

int main() { srand(unsigned(time(nullptr))); const wchar_t* fileName = LR"(D:\logs\log1.txt)";

// Create file with shared read/write permission
HANDLE h = CreateFile(fileName,
    GENERIC_WRITE,                      // open for writing
    FILE_SHARE_READ | FILE_SHARE_WRITE, // share for reading/writing
    NULL,                               // default security
    CREATE_NEW,
    FILE_ATTRIBUTE_NORMAL,              // normal file
    NULL);                              // no attr. template

if (h) {
    for (int i = 0; i &lt; 100; i++) {
        DWORD n;
        char str[64];
        int l = sprintf_s(str, sizeof str, &quot;%d: %d\n&quot;, i, rand());
        WriteFile(h, str, l, &amp;n, NULL); FlushFileBuffers(h);
        Sleep(3000);
    }
    CloseHandle(h);
}

}

It creates log1.txt and writes to it periodically. I also have the below PowerShell script to create another shared log file

$fs = [IO.FileStream]::new('D:\logs\log2.txt', `
    [IO.FileMode]::OpenOrCreate, [IO.FileAccess]::Write, [IO.FileShare]::ReadWrite)
for ($i = 0; $i -lt 100; $i++) {
    $t = [Text.Encoding]::ASCII.GetBytes("$i $(Get-Random)`r`n")
    $fs.Write($t, 0, $t.Length); $fs.Flush()
    Sleep 3
}
$fs.Close()

After that I ran the command on top and all the log files are cleared

phuclv
  • 30,396
  • 15
  • 136
  • 260
0

Save the below code as a batch file and Run it.

You can save the trouble of typing the command everytime :).

Change M:\Newfolder\ & *.txt to reflect your path & log file extension respectively.

@ECHO OFF
setlocal EnableDelayedExpansion
FOR /R M:\Newfolder\ %%G IN (*.txt) DO (
    TYPE nul > %%G
)
pause

UPDATE

You can try to create a Macro to automate what you want to do.

Check out the first answer provided by ellak here

Open all your Log Files at once and then Run this Macro Everytime.

If you want to automate even that (opening all your Log Files at once) then Run the Batch Script below.

@ECHO OFF
setlocal EnableDelayedExpansion
FOR /R M:\Newfolder\ %%G IN (*.txt) DO (
    notepad++.exe %%G
)
pause

So the next time you want to clear your Log Files, Simply

  1. Run the Batch Script provided
  2. Run the Macro from Notepad++

There is a way to run them both from the same script but this requires NPPExec Plugins that I'm not too familiar with. Please let me know if you want it with the plugin or if the solution provided above will suffice.

phuclv
  • 30,396
  • 15
  • 136
  • 260
0

Just a thought if the file is being used and you can't clear it, and Notepad++ is allowing you to clear it why not just use a script in Notepad++ like @Dhiwakar Ravikumar mentioned?

If scripts are out of the question, and assuming you have quite a bit of log files, why not just open them all in Notepad++, select the first open tab and hit Ctrl+A, then Delete key, then Ctrl+S, then Ctrl+W? That would select all the text in the file, delete it, save the file, then close it and then just do the same thing (like a script)

phuclv
  • 30,396
  • 15
  • 136
  • 260
trenten
  • 155
-1

From a command line:

FOR /R C:\YourFolder %f IN (*.LOG) DO TYPE NUL>%f

Or, from a batch file:

FOR /R C:\YourFolder %%f IN (*.LOG) DO TYPE NUL>%%f
aphoria
  • 984