Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Find Unused Files: v1.4.0

4.16/5 (8 votes)
20 Sep 2015CPOL6 min read 54.4K  
Finds unused files and images in your website projects.

Helps you find and eliminate unused images & files in web projects/applications.


Latest Changes:

v1.4.0

  • Made picturebox a little bigger, and moved outside of bounds of main window for better ergonomics
  • Added image-preview checkbox to not have to alt-click on an image to trigger display
  • Added some extra checking for file path operations
  • Fixed some thread-safety issues
  • Added "Launch External" to context menu to launch a file with it's associated external program
  • Switched search method, 2x faster, especially on extra large folders (2000+ files)
  • Ordered files in list
  • Prevent from searching for a reference of oneself (foo.css inside foo.css)
  • Made form resizable
  • Added button to cancel a search in progress in case of long running instance
  • Added pseudo-dropdown to pattern textboxes
    • Items are auto-added after a sucessful search
    • Items can be deleted via a right-click on the textbox
  • Added update check in help menu
  • Added auto-update check to option panel
  • Added exclusion list to option panel
    • Can be file names or folders (backslash)
    • Anything matched will be ignored
  • Added a treeview pane to see in which files ressources are used in
    • Has some context menus, but some things like multi-select do not work in treeviews, thus the Flat View pane that remains
  • Fixed Visual Studio from crashing when double-clicking a file
    • This was happeneing when "track active item in solution explorer" was already checked in VS options
  • Switched .csproj back to VS2013 temporarily to fix some issues on 2013. Has no impact on 2015

Introduction

The other day at work I was cleaning up a project and got frustrated because i wanted to remove all images that are no longer referenced inside CSHTML, CSS, and LESS files ..but couldn't find any soft or plugin that would help me, so I got stuck with selecting each image name one by one and then searching through the entire project for each file to see if I got a match or not ..Grrrrr!

So I decided to whip up a little soft to help automate just that.  

Works as a Standalone Application, from the Windows Explorer context menu, from the Command Line, or as a Visual Studio extension. Thank's to the command line it can easily be integrated with your favorite development IDE too. 

 

Flat View

Tree View

File/Folder Exclusions

Thumbnail Preview

 

Parsing Dialog

 

 

 Windows Explorer (context menu)  

 

 

Visual Studio (context menu)

 

 

The process was really easy, so unless i missed something, i'm surprised i couldn't find some program on the web that already did just that.

Anyways here's how it works: 

  1. enter a regexp that matches files you want to match
  2. choose if you want to match if they are used or not used
  3. choose what filetypes you want to search within to see if they have references in there
  4. select your starting directory 
  5. ......processing 

The simple logic behind it:

  1. load all file names from base directory & sub-directories
  2. extract all file names that match what we are looking for
  3. extract all file names that match which files we will be searching in
  4. iterate over each of the files we are searching in
  5. open each file and iterate over the content
  6. check if the content contains a match and add to a match list if so
  7. do a diff between matched and all

That's all, and it's pretty fast too. And since it uses standard regexp as input, you can match pretty much anything in any type of file. 

Features: 

  • Can be launched via the command line. Like this it's easy to integrate with Visual Studio, Eclipse, Zend Studio, NetBeans, IntelliJ IDEA, etc.  
  • Double clicking a path in the visible list will open the directory the file is contained in
  • Once file processing is done you can toggle the visible list between used/unused lists
  • Select items in list and export the paths to a text file for later investigation 
  • Regexp criteria is saved after successful search and restored on application launch
  • Selecting one or more paths in the visible list and clicking delete, will prompt you if you really want to delete the file(s)
  •   

Requires .NET 4 for the Application or .NET 4.5 / Visual Studio 2012 (and up) for the Plug-in



Change log  

v1.4.0

  • Made picturebox a little bigger, and moved outside of bounds of main window for better ergonomics
  • Added image-preview checkbox to not have to alt-click on an image to trigger display
  • Added some extra checking for file path operations
  • Fixed some thread-safety issues
  • Added "Launch External" to context menu to launch a file with it's associated external program
  • Switched search method, 2x faster, especially on extra large folders (2000+ files)
  • Ordered files in list
  • Prevent from searching for a reference of oneself (foo.css inside foo.css)
  • Made form resizable
  • Added button to cancel a search in progress in case of long running instance
  • Added pseudo-dropdown to pattern textboxes
    • Items are auto-added after a sucessful search
    • Items can be deleted via a right-click on the textbox
  • Added update check in help menu
  • Added auto-update check to option panel
  • Added exclusion list to option panel
    • Can be file names or folders (backslash)
    • Anything matched will be ignored
  • Added a treeview pane to see in which files ressources are used in
    • Has some context menus, but some things like multi-select do not work in treeviews, thus the Flat View pane that remains
  • Fixed Visual Studio from crashing when double-clicking a file
    • This was happeneing when "track active item in solution explorer" was already checked in VS options
  • Switched .csproj back to VS2013 temporarily to fix some issues on 2013. Has no impact on 2015

v1.3.1

  • Added SVG, CSS, JS to default item list
  • Added scss to default files list
  • Made search culture-invariant / case-insensitive
  • (extension) Added Visual Studio: Enterprise & Community to supported platform list
  • (extension) Solution (.sln) will now only build with Visual Studio 2015
    • But plug-in will work with Visual Studio 2012 and up
  • Released Source-Code on Github

v1.3

  • Alt-Click in list will show image thumbnail if it's an image
  • Double-Click on list will open & set focus to item in solution explorer (VSIX only)
  • Shell integration (EXE only)
    • Add/Remove from Help menu, or register/unregister from command line with -s t
  • More file stats

v1.2

  • Changed UI a bit
  • Context menus
    • Select All
    • Select Other
    • Export Selection
    • Delete Selection
    • Explore Here
    • Verify Selection (VSIX only: launches automated VS file search)
      • In Project
      • In Solution
  • Command line parameters (EXE only)
    • -p path
    • -r report type
    • -f find regexp
    • -i inside regexp
  • Help Menu
    • Manual
    • Change-log
    • Website
    • About
    • License
  • More file stats
  • More error handling  

v1.1

  • Disable delete button if no are files selected
  • Enabled searching in website projects (VSIX)
  • More error messages on invalid and/or unsuccessful operations

 

v1.0 (Visual Studio Plug-in)

  • .EXE and VSIX versions now share same code-base
  • Various modifications to accommodate different behavior in Visual Studio Mode & Standalone Mode
  • Enabled searching in application projects

 

v1.0 (Application)

  • Toggle between used/unused lists
  • Export used/unused lists
  • Delete file(s) with confirmation prompt
  • Save search criteria after search
  • Show errors on invalid regexps
  • Restore search criteria on application launch
  • Double click used/unused list opens containing directory
  • Allow passing file/directory path via command line 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)