randle Posted November 19, 2013 Posted November 19, 2013 Hi, I'm looking for some code to delete all files if their content is empty. I have been using 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?
pcstru Posted November 19, 2013 Posted November 19, 2013 If they have a length > 0, then they are not empty. Presumably you mean they are content free and contain only white space?
randle Posted November 19, 2013 Author Posted November 19, 2013 (edited) 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. Edited November 19, 2013 by randle
LosOjos Posted November 19, 2013 Posted November 19, 2013 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.
pcstru Posted November 19, 2013 Posted November 19, 2013 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.
randle Posted November 19, 2013 Author Posted November 19, 2013 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.
LosOjos Posted November 19, 2013 Posted November 19, 2013 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
pcstru Posted November 19, 2013 Posted November 19, 2013 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.
randle Posted November 19, 2013 Author Posted November 19, 2013 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.
randle Posted November 19, 2013 Author Posted November 19, 2013 Ok I feel I'm getting there but can't quite finalise this at the moment. So far I have 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.
pcstru Posted November 19, 2013 Posted November 19, 2013 (edited) 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 : $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. Edited November 19, 2013 by pcstru
randle Posted November 21, 2013 Author Posted November 21, 2013 Thanks for the help but managed to get this working with the following code in the end 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.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now