Skip navigation

Cleaning Up Unwanted Files, the One-Liner Way

I've recently found myself using a software program that litters my project directory structures with "hidden" files. These all have a name like "._*" and they're used for the application to store its metadata in - even though I've repeatedly told it to NOT do that. It's annoying because I have to copy these files to customers, and they don't want to see all the stupid metadata folders, which don't stay hidden on the customers' file systems.

So I hacked out a quick PowerShell one-liner to delete them all.

PS H:\> dir -recurse | where-object -filterscript { $_.gettype().name -eq 'DirectoryInfo' } | foreach-object -process { $_.getfiles() } | where-object -filterscript { $_.name -like '._*' }  | ForEach-Object -process { del -path $_.fullname -force }

This, I think, is a great illustration of how one-liners can get a job done without much scripting. 
  • I start by getting the directory from the current location (I have an H: drive mapped to it), and recursing subfolders.
  • Where-Object only keeps those objects of the type DirectoryInfo. Keep in mind that Dir can also return FileInfo objects in the file system, so I'm basically discarding those.
  • Whatever's left gets fed to ForEach-Object, which then retrieves the files from each folder. Looking at this now, I'm not sure why I didn't just elect to keep objects of the FileInfo type. Huh. Oh well, it works and I'm not changing it now. You can post your improved version in the comments :).
  • Next, I keep only those files that match the naming criteria: ._* (* is a wildcard, obviously)
  • For those files, I delete them. I think I could probably have called a Delete() method of the file, rather than calling the Del command and giving it the file's FullName property, but whatever. Gets the job done. 
This is something a normal person would save into a .PS1 file and run whenever they needed it. I also just realized that I have it in a .TXT file on my desktop and that I've been copying and pasting it every time I need to run it. Weird. I'll go rename the file and put it somewhere a bit more easily accessible, or maybe rewrite it into a function and stick it inside the script module that I have the shell load at startup.

You know, the other point to make here is that when I "hack" something out, I really hack it out ;). I'm pretty careful when I'm teaching classes, but when I'm just trying to get stuff done on my own I'm happy for the result to be as ugly as it needs to be. Whatever gets the job done first, wins, and I rarely look back. There WAS a good reason why I got directories first and then got the files from each one, although for the life of me I can't remember that reason right now. I'll have to get back to you on that.
Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish