+ Post New Thread
Results 1 to 12 of 12
Scripts Thread, Powershell: code to delete empty files in Coding and Web Development; Hi, I'm looking for some code to delete all files if their content is empty. I have been using Code: ...
  1. #1
    randle's Avatar
    Join Date
    Dec 2006
    Location
    Chelmsford, Essex
    Posts
    595
    Thank Post
    66
    Thanked 16 Times in 15 Posts
    Rep Power
    19

    Powershell: code to delete empty files

    Hi,

    I'm looking for some code to delete all files if their content is empty.

    I have been using
    Code:
    get-childItem "PathToFiles" | where {$_.length -eq 0} | remove-Item
    but I'm finding instances where empty files still have a byte count and so this command misses them out.

    Any ideas?

  2. #2


    Join Date
    May 2009
    Posts
    3,201
    Thank Post
    283
    Thanked 861 Times in 644 Posts
    Rep Power
    336
    If they have a length > 0, then they are not empty. Presumably you mean they are content free and contain only white space?

  3. #3
    randle's Avatar
    Join Date
    Dec 2006
    Location
    Chelmsford, Essex
    Posts
    595
    Thank Post
    66
    Thanked 16 Times in 15 Posts
    Rep Power
    19
    Sorry, I should have made it clearer.

    I was using the above code as this was removing files which had no content but am now finding files present with just blank space which still has a byte count but still need to be removed (the files not the spaces) so the above code no longer works as desired.
    Last edited by randle; 19th November 2013 at 10:41 AM.

  4. #4

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,528
    Thank Post
    1,460
    Thanked 1,214 Times in 823 Posts
    Rep Power
    724
    A file isn't necessarily "empty" just because it contains only white space when viewed in a text editor, it may well contain binary data that is needed by some application. I wouldn't suggest deleting anything that contains any data, the file size will be negligible but you could break some software.

  5. #5


    Join Date
    May 2009
    Posts
    3,201
    Thank Post
    283
    Thanked 861 Times in 644 Posts
    Rep Power
    336
    I think you will need to write a function that examines the file and checks the content. If it is always the same issue - files with one space, then they should have a set length (not necessarily 1, they may have cr/lf or other non display characters), so you could use your command to pipe those files to your function. As LosOjos says, there is a risk you might break something. Sounds like a lot of effort for very little reward IMO.

  6. #6
    randle's Avatar
    Join Date
    Dec 2006
    Location
    Chelmsford, Essex
    Posts
    595
    Thank Post
    66
    Thanked 16 Times in 15 Posts
    Rep Power
    19
    I appreciate the concern but in this instance I know what this file is used for and will not break anything unknown to me. This is not a Windows system file and unless it has the content expected rather than just blank space, it's useless. Removing this is not little reward as is part of a larger script and extremely useful for use here!

    I'm open to alternative mothods to remove these files but was hoping for something similar to what I'm already using as most of the time this does what I want.

  7. #7

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,528
    Thank Post
    1,460
    Thanked 1,214 Times in 823 Posts
    Rep Power
    724
    Quote Originally Posted by randle View Post
    I appreciate the concern but in this instance I know what this file is used for and will not break anything unknown to me. This is not a Windows system file and unless it has the content expected rather than just blank space, it's useless. Removing this is not little reward as is part of a larger script and extremely useful for use here!

    I'm open to alternative mothods to remove these files but was hoping for something similar to what I'm already using as most of the time this does what I want.
    Assuming then that there is some sort of consistency to it, could you perhaps check for a string of text within the document and delete it if it's missing? I'm afraid I know nothing about PowerShell but if I was writing this script, that's something I'd be looking at

  8. #8


    Join Date
    May 2009
    Posts
    3,201
    Thank Post
    283
    Thanked 861 Times in 644 Posts
    Rep Power
    336
    Ok. You know what you are doing! :-)

    So you could use your command to identify files that are candidate (you know the size or perhaps you know they are 5 bytes or less). I think you could then try to use select-string with a regex to confirm the file is what you want and use the output of that to pipe through to a delete. That will save you writing a function and might keep it to one line.

  9. #9
    randle's Avatar
    Join Date
    Dec 2006
    Location
    Chelmsford, Essex
    Posts
    595
    Thank Post
    66
    Thanked 16 Times in 15 Posts
    Rep Power
    19
    Quote Originally Posted by LosOjos View Post
    Assuming then that there is some sort of consistency to it, could you perhaps check for a string of text within the document and delete it if it's missing? I'm afraid I know nothing about PowerShell but if I was writing this script, that's something I'd be looking at
    That should do the job actually. Just need to find out the syntax to check the file for not containing specific content.

    @pcstru: The problem is that blank files are showing the same file size as the non-corrupt files so this comparison isn't ideal in this scenario.

  10. #10
    randle's Avatar
    Join Date
    Dec 2006
    Location
    Chelmsford, Essex
    Posts
    595
    Thank Post
    66
    Thanked 16 Times in 15 Posts
    Rep Power
    19
    Ok I feel I'm getting there but can't quite finalise this at the moment. So far I have
    Code:
    get-childitem D:\Temp\ret\* -include *.old -recurse | select-string -pattern "configuration" -notmatch | remove-Item
    Going by the PS example given from get-Help, this should delete any files that don't have the word "configuration" in them however, when running this I get an IOException saying that the process cannot access the file. Still looking into this at the moment but if you see anything obvious that I'm doing wrong just shout.

  11. #11


    Join Date
    May 2009
    Posts
    3,201
    Thank Post
    283
    Thanked 861 Times in 644 Posts
    Rep Power
    336
    Mmmm.. select string outputs file name as one of it's properties[*1], another is the line that is the match (or not match). So the file is still open for read by select-string when it is passed to remove-item because the pipe is feeding the next component (remove-item) before it has finished with the previous (reading the item). I think. You might need two passes. Get the file paths into an array first and then delete them once that is complete.

    i.e :
    Code:
    $files=get-childitem c:\tmp\* -include * -recurse | select-string -pattern "configuration" -notmatch | Select-Object Path 
    $files | foreach-object { remove-item $_.Path }
    You may still get errors because each file may be in the array multiple times.

    [*1] Always useful to pipe something to get-member to see what the object is made of.
    Last edited by pcstru; 19th November 2013 at 06:26 PM.

  12. #12
    randle's Avatar
    Join Date
    Dec 2006
    Location
    Chelmsford, Essex
    Posts
    595
    Thank Post
    66
    Thanked 16 Times in 15 Posts
    Rep Power
    19
    Thanks for the help but managed to get this working with the following code in the end
    Code:
    get-childitem $dal -include *.old -recurse | where {-not(select-string -path $_.FullName -pattern "configuration")} | remove-Item
    Appears to work a treat

    Apparently -NotMatch doesn't work the way I was trying to use it. It's for finding all lines that don't match a pattern, not for finding files that don't contain any lines matching the pattern.

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 2
    Last Post: 27th September 2013, 11:22 PM
  2. Scripting to Delete multiple files and folders.
    By mdixon123 in forum Scripts
    Replies: 8
    Last Post: 2nd January 2011, 07:28 PM
  3. Script to delete certain files
    By tri_94 in forum Scripts
    Replies: 9
    Last Post: 30th April 2008, 04:56 PM
  4. Replies: 5
    Last Post: 9th March 2008, 12:15 AM
  5. Replies: 1
    Last Post: 21st January 2007, 02:51 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •