Visual Studio - Remove unused files - visual-studio

Was wondering if there's an extension or macro or something that looks through your solution and gives out a report of which js/css/image files are not being referenced anywhere in code?
I have a large project and over time it has accumulated dust. Other than manually searching for each file's usage, is there any other time saving way?

There's a Visual Studio Extension that searches for unreferenced image files. It finds all image files in your project and then scan all aspx/cs/ashx/css/js files for references. It has not yet been updated for Visual Studio 2012.
http://visualstudiogallery.msdn.microsoft.com/fb7a9b9c-08e1-4bb4-91b4-8e512feb5a1b

Update You will not find a tool that can systematically identify unused resources, because JavaScript, CSS, and image files can be loaded dynamically. This article shows how to load JavaScript and CSS dynamically, and it's a straightforward task in JavaScript to load an image dynamically. It's easy to imagine a scenario in which the image name is loaded from an external data source, or concatenated from another field value plus ".jpg". Clearly any tool that attempted to scan your source to find unreferenced files would miss these resources.
That said, you can search for hard-coded references to .js, .css, and .jpg files using Visual Studio's search by regular expression feature, or by using a high-powered text editor like Notepad++ with a Regular Expression search feature.
For example, to use Visual Studio to search for all files ending in .js that are referenced in ASCX ir ASPX pages, go to Edit/Find and Replace/Find in Files, set the search expression to .js> switch on
"Use Regular Expressions", and set "Look in these file types" to "*.aspx; *.ascx".
("\" escapes the ".", and ">" means end of word, so that "foo.js" is found, but not "foo.jsx". Visual Studio has its own Regular Expression syntax, which is documented here)
In addition, the tools in my original response below can give a good picture of what JS/CSS/IMAGE resources are actually getting used when your site is loaded. When used in conjunction with a testing tool like Selenium, these should allow you to remove resources with confidence.
There are several tools you should look at:
WARI--Web Application Resource Inspector seems the closest fit. According to their website:
WARI scans your web application and examines dependencies between JavaScript functions, CSS styles, HTML tags and images.
The goal is to find unused images as well as unused and duplicated JavaScript functions and CSS styles.
JSCoverage is a code coverage inspector for Javascript.
For CSS, there is an online tool at http://unused-css.com/ and a Firefox extension called DustMeSelectors

The extension in the selected answer above only works in vs2012 while Code Maid works in vs2010 - vs2014:
There is a free extension called Code Maid that "is an open source Visual Studio extension to cleanup, dig through and simplify our C#, C++, F#, VB, XAML, XML, ASP, HTML, CSS, LESS, SCSS, JavaScript and TypeScript coding." Does images as well.

Solution Features
is semi - manual
isn't an extension
looks long but is not complicated or difficult
works for just images or all files
overcomes all the ajax tricks
should work with any project or version of Visual Studio
Steps
Run the website in Chrome with debugging turned on (press F12 before launching website)
Completely exercise the website. Make it download everything that it will ever download.
Go to the Network tab.
On the file grid, Click on any of the images that appear in the list (doesn't matter which one). This will make all column headers but Name go away.
Click on the Name column header, to sort ascending.
Do CTRL+A, CTRL+C to copy all file names.
Paste into a new Google spreadsheet (in google docs) into cell A2.
Repeat steps 6 and 7 if for any reason it didn't copy all the file names. Scroll down.
Go into your actual website images directory (or whatever directory you are interested in comparing) in a command prompt and issue dir /b. Copy this into the clipboard (mark function) and paste into cell B2.
In cell C2, paste this formula =not(isna(VLOOKUP(B2,$A$2:$A$TheBottomOfA,1,false))). Alter TheBottomOfA to be the last used row in column A.
Copy the formula down for all your values in column B.
In cell C1, type Is Used. In cell B1, type Name. Add a data filter on all the values in columns B and C. Set the filter with the mouse in C1 to show only Is Used=FALSE.
Result
What you're looking at in Column B is a list of files you are not using.
Note
I recommend moving the unused files to an offline folder instead of deleting them.
Warning
You still need to use common sense. BRAIN=ON

2022 solution
you can use this extension in vscode to find all unused exports

If you're using a new version of Visual Studio and can't use the extensions, what I did was this:
Exclude all target files.
Find ",,," (this indicates an absolute resource reference) and include each resource found. Note, you should try any unique keywords relevant to your references.
Repeat step 2 until Visual Studio stops opening files (if no new matches are found, already opened documents will come into focus).
Try building project. If you happen to miss a resource, Visual Studio will let you know. In that case, repeat steps 2-4; it is worth nothing this will rarely ever be necessary.
I've only had to do this once and I spent ten minutes, at most.

Related

Exclude folder(s) in VS 2019 Find in Files

...yes I know this is an old tune, but I would like to make it specific to the newest tool, and also put in context and emphasize the issue. Doing this in hope that soon we will have a working solution.
Context
In many web projects there are zillions of library files like jquery, bootstrap etc. To make it worst, there are the .min. siblings, what are one liner, so editor killers. Usually we do not want to search within those files and it is a productivity killer, especially if one accidentally clicks on a found .min. file which freezes the editor when opens...
Question
Is there any way to define and exclude folders in a project or solution when using VS 2019 Find in Files?
If you press the button to the right of the "Look In" drop down:
you can select a set of folders to use. This set of folders can be saved for future use (ie. the lack of ability to include a parent folder and then exclude a child is not as bad as it could be).

How to exclude folder from Visual Studio 2013 Find?

I am using an ASP MVC project. Both the /Content and the /Script folder has a Kendo subfolder which contains enormous sized files. As a consequence when I am using solutionwide Find operation it takes literally minute(s) and even the VS UI is frozen and became unresponsive (btw this is a shame in 2015, I am using a 8 core / 16Gigs machine and all files on SSD, anyway).
As a workaround it would be great to exclude the 2 Kendo subfolders from Find operations. How can I accomplish this?
Note: using file wildcards, and not searching in any .css and .js files is not an option, because I have my own .js and .css files and searching in those files is mandatory.
In order to exclude subfolders, you can create a custom Folder Set. Click the ellipse (...) for the "Look in" option in the "Find and Replace" dialog.
Related answer: Visual Studio Search in Selected Folders or File Types Only
If the problem is that the gui is lagging. Try Find In Files -> Result Options -> Display file names only = true. Hopefully that is good enough.

How to write a designer extension or editor extension with generated file as child?

I am trying to figure out how to make an editor extension (like T4 templates) or designer extension (like a WinForms designer) that produces a generated file and a "code-behind" file.
For the T4-style code generator I know that a single-file generator is an option but it is very limited (e.g. only a single output file is allowed) and I am not sure how to combine that development path (which seems incompatible with vsix deployment) with an extension that supports other features such as syntax highlighting or editor context menus.
So I would like to find a vsix-compatible approach. I would like to write an extension that works like this
Offer a new *.xyz file extension in the New file dialog
Upon creating the new *.xyz file, a text editor or custom editor (personally I want to use a text editor) appears to edit the file.
When the *.xyz file is edited, one or more *.cs (or let's say *.vb files in a VB project) pop out as children of the *.xyz file in solution explorer and become part of the project. (I am also curious about how you can make one of the files non-generated, like the user-editable code-behind file that comes with a *.xaml file, but for my own purposes I only need to produce generated file(s).)
I know how to do #2 (syntax highlighting, and I saw something on MSDN about custom editors). #1 is a lower priority but I'm haven't quite figured it out yet; e.g. there are instructions on MSDN but no mention of how you would integrate an item template into a VSIX project.
The most important thing I want to know is #3. Does this feature even have a name I could google for?

re-including the entire contents of a folder in visual studio 2013

I have an included folder in visual studio, which contains mainly javascript files, CSS, images, etc. These are not required for the website to run, and are altered not from Visual Studio but from other external processes.
I've been publishing to Windows Azure Websites, which require all files to be included so that they are uploaded as part of the package.
Is it possible to somehow click on a folder in the Solution Explorer, and 're-include' all it's contents?
The only option I found right now is to exclude and re-include, which due to the amount of files takes quite some time. It takes ages to 'exclude' the folder. Then, once it's ready I have to re-include which takes a long time as well. Would like to at least do them in one operation.
Was wondering if there was a better process somehow.
I'm not quite sure why you have exclude them all and include them again. Is it because the file names change or new files created often? If the file names stay the same and just the contents are changing you should not have to re-include them. If the external processes create new files now and then, then you should be able to click the "Show all files" option in solution explorer and then when you load the solution check the directory in solution explorer and any new files there include them.
hope that helps in some way. :)

Excluding specific files from Visual Studio search

Is it possible to exclude certain files from search in Visual Studio.
For example jquery.js is almost always polluting my search results with half result coming from that file.
I know you can white-list specific types, but when I want to search in .js extension is there solution for that?
Vote here for feature: https://developercommunity.visualstudio.com/idea/405990/code-search-exclude-files-from-search.html?inRegister=true
Altough it does not solve your problem it may help out a bit
Ctrl + Shift + F should trigger the Find and Replace window.
From there, click Result Options and select "Display file names only".
It won't have all the info you need but might make it easier to recognize the files.
In Visual Studio 2019 they modernized the "find in files" feature, now you can exclude files, file extensions and directories using an exclamation mark before the items to be excluded in the "File types" textbox, like this:
*.*;!jquery.js
another example:
!*\bin\*;!*\obj\*;!*\.*;!*.xml
More info: https://devblogs.microsoft.com/visualstudio/modernizing-find-in-files/
In Visual Studio 2017 there is a workaround: you can right-click a search result and then click Delete. I use it to eliminate the big minified files from the Find Results window.
I've got the same problem with unwanted .js files polluting the search result. Especially the minified versions (e.g. jquery.min.js) are really annoying since they consist of only one (1) single very very long line. All of that line is displayed line-wrapped in search result. Not ideal!
Possible solutions:
Since .js files are (normally) just static content, you should be able to name them as you like. Rename it to jquery.min.js.nosearch and include the file with <script type="text/javascript" src="jquery.min.js.nosearch"></script> in HTML.
Get these files from an CDN and delete your local files.
Exclude these files from the VS project, provided that you can handle the inclusion of them in an other way when needed, e.g. when deploying (and provided that you scope your search to solution/project, not folder).
From this answer there was an UltraFind extension, which unfortunately doesn't exist for newer than 2010 (but see thread for hack to "update" it to 2012)
It's not particularly elegant - I'd be reluctant to call it a solution to the question - but if you can have Visual Studio Code running side-by-side with VS201x, its Find and Replace feature is pretty sophisticated. If you're using Git for source control, it will exclude any files or folders found in .gitignore from its search results - this is great when used in conjunction with tools like LibMan. Failing that, you can always manually add files / folders to its "files to exclude" option when searching.

Resources