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?

5
  • 3
    What OS are you using?
    – aphoria
    Oct 22, 2014 at 11:25
  • You want something like for f in *.log; do :>"$f"; done or the Windows equivalent.
    – AFH
    Oct 22, 2014 at 11:26
  • 1
    I use Wnidows OS Oct 22, 2014 at 11:37
  • 1
    What's Wnidows OS? Oct 22, 2014 at 11:44
  • 1
    I use Windows 7 Oct 22, 2014 at 11:45

4 Answers 4

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 < 100; i++) {
            DWORD n;
            char str[64];
            int l = sprintf_s(str, sizeof str, "%d: %d\n", i, rand());
            WriteFile(h, str, l, &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

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.

8
  • The process cannot access the file because it is being used by another process Oct 22, 2014 at 12:22
  • That means the file is being used. You can't clear the contents of a file that you have opened (in notepad or some other application) Oct 22, 2014 at 12:23
  • At this moment I can open this file using notepade++ and clear content and save file Oct 22, 2014 at 12:58
  • 1
    I thought the purpose of asking the question was to avoid doing it manually. Please try to find out the Application using these Logs. End it and run the script again. Oct 22, 2014 at 13:36
  • Yes, I want to avoid make it manually. Application server keep these logs files. And I don't want to stop it. hotepade ++ allow this Oct 22, 2014 at 13:45
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)

-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
8
  • The process cannot access the file because it is being used by another process. Oct 22, 2014 at 12:19
  • 1
    How you clearing it manually?
    – aphoria
    Oct 22, 2014 at 12:22
  • I open this file using notepade++ and clear content and save file Oct 22, 2014 at 12:59
  • 1
    If another process has the file locked, I'm not sure how you can be clearing and saving it.
    – aphoria
    Oct 22, 2014 at 16:56
  • 1
    I have tried a lot of methods on this and they all involve doing quite a lot of work (installing software or programming). Your simplest answer is to boot into safe mode (command-line only) and the above solutions should work. This will be fine for a one-off, though not something to do regularly. I tried NP++ on a file held open for writing and got an error, effectively the same as you saw, so I don't know how it works for you. Which logs are you talking about?
    – AFH
    Oct 22, 2014 at 17:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .