empty a file that is in use

Welcome Forums General PowerShell Q&A empty a file that is in use

This topic contains 7 replies, has 5 voices, and was last updated by

 
Participant
2 months, 3 weeks ago.

  • Author
    Posts
  • #162579

    Participant
    Topics: 2
    Replies: 4
    Points: 17
    Rank: Member

    Hi all,

    I'm trying to empty a log file that is in use by some process, I'm using Clear-Content but I get the followin error:

    Clear-Content : The process cannot access the file 'C:\temp\XXX.txt' because it is being used by another process.

    Anyone knows if exists a way to empty files like these without find and stop any process?.

     

    Many thnaks.

  • #162596

    Senior Moderator
    Topics: 8
    Replies: 1040
    Points: 3,433
    Helping Hand
    Rank: Community Hero

    Well,

    Why do you want to empty that file when it is in use. You have mentioned about "without stopping process", so I guess the process is important, hence th log file which is written by the process as well. Can you give some more details on what you are trying to do ?

  • #162602

    Participant
    Topics: 2
    Replies: 4
    Points: 17
    Rank: Member

    Well,

    Why do you want to empty that file when it is in use. You have mentioned about "without stopping process", so I guess the process is important, hence th log file which is written by the process as well. Can you give some more details on what you are trying to do ?

    Hi Kvprasoon,

    I need to empty some file because we have limited space on our system drive and we don't have control to the application that create these files. I think these files are not important because they ara stores on temp folder and they are too big to be open with notepad for example. I belive there are a third-party windows service blocking the file and that's why I want to clear these files.

    Thx.

  • #162609

    Participant
    Topics: 8
    Replies: 31
    Points: 116
    Helping Hand
    Rank: Participant

    Since it's got a file lock at the OS level, you could try using openfiles.exe to close the file, then empty it immediately, hopefully before the process locks the file again.

  • #162624

    Participant
    Topics: 2
    Replies: 54
    Points: 277
    Helping Hand
    Rank: Contributor

    Frankly, this sounds like it's not really a PowerShell issue, but rather a Windows-handles-permissions-poorly issue. Who does have control over the application? What application is it? Can you possibly trigger it to start writing to a new log file? Does the application load automatically? Can you reboot the system and get to the file before the application starts?

    If all else fails, you could probably load the system in Safe Mode to the command prompt only (no GUI) and get at the file that way.

  • #162689

    Participant
    Topics: 12
    Replies: 232
    Points: 466
    Helping Hand
    Rank: Contributor

    This is really up to the application that locks the file and the underlying OS.

    So the best way would be to check with the application creator to handle the log file pruning or to release the file when it's not logging.
    All other methods would be hackish and could potentially lead to errors down the line.
    It's the whole point of locking a file for writing to avoid data corruption.
    So even if it's an unimportant file you never know how the application will handle things being removed while it's writing.

    The only safe way would be to stop the application/service using the file, delete, start the service.
    Maybe do it as a step during the backup window etc.

  • #162738

    Participant
    Topics: 2
    Replies: 4
    Points: 17
    Rank: Member

    Hi all,

    Finaly we decided to open a case with the app owner. I hope there exists a way from the app side to reduce this files length.

    Many thanks to all for your replys.

  • #162762

    Participant
    Topics: 12
    Replies: 232
    Points: 466
    Helping Hand
    Rank: Contributor

    Sounds like the best option.

    It shouldn't be that difficult and there are lots of options.
    E.g. just setting a threshold size and rollover to a new active log file once it's reached, releasing the lock on the old file.

  • #172408

    Participant
    Topics: 0
    Replies: 1
    Points: 7
    Rank: Member

    Hi All,

     

    Facing the exact same issue, without any apps or other users touching the text file, only changing the content of it manually in text editor, so for me it seems rather OS or PS issue afterall:/

    @Javier, did you manage to sort this out somehow?

You must be logged in to reply to this topic.