Just recently I've come across a problem where when I want to update my program to a new version, (Usually I would hit Ctrl+F and change all references i.e. 1.0 to 1.1) but now I've added my program's website to the solution, and I don't want the versions there to update.
I want to be able to exclude this project from Find-And-Replace or, even better, exclude all image files and html files.
Whichever is more efficient.
Enter in Ctrl+Shift+F and type in *.cs under file type field.
Related
...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).
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.
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.
I have to "look in:" a subfolder of the project because the entire project is very large and takes too long to search through.
I also have AnkhSVN installed and wonder if a setting in the plugin could help too.
If you use "Find in files" instead of the standard search, you can search a subfolder for file types you specify.
However, it's a lot easier to perform this kind of task using the Ultrafind add-on (http://visualstudiogallery.msdn.microsoft.com/9fa9fdd7-1c06-45e3-a9f3-0381caab8f94) which you can use to exclude specific file patterns.
Sadly, it seems that despite all the wonderful functionality of Visual Studio, the easiest way to omit .svn directories from searches is to use Windows Explorer to navigate to the .svn directory, right click the folder, go to Properties, and click the Hidden checkbox under Properties.
If you then re-open your Visual Studio solution, it should keep those files out of the searches.
A quick and dirty way is to simply include every other file type except .svn and .svn-base etc.
Try using this set of wildcards and add any other valid extensions beginning with S that you might need:
*.sql;*.svc;*.;*.?;*.??;*.a??;*.a???;*.a????;*.b??;*.b???;*.b????;*.c??;*.c???;*.c????;*.d??;*.d???;*.d????;*.e??;*.e???;*.e????;*.f??;*.f???;*.f????;*.g??;*.g???;*.g????;*.h??;*.h???;*.h????;*.i??;*.i???;*.i????;*.j??;*.j???;*.j????;*.k??;*.k???;*.k????;*.l??;*.l???;*.l????;*.m??;*.m???;*.m????;*.n??;*.n???;*.n????;*.o??;*.o???;*.o????;*.p??;*.p???;*.p????;*.q??;*.q???;*.q????;*.r??;*.r???;*.r????;*.t??;*.t???;*.t????;*.u??;*.u???;*.u????;*.v??;*.v???;*.v????;*.w??;*.w???;*.w????;*.x??;*.x???;*.x????;*.y??;*.y???;*.y????;*.z??;*.z???;*.z????;*.0??;*.0???;*.0????;*.1??;*.1???;*.1????;*.2??;*.2???;*.2????;*.3??;*.3???;*.3????;*.4??;*.4???;*.4????;*.5??;*.5???;*.5????;*.6??;*.6???;*.6????;*.7??;*.7???;*.7????;*.8??;*.8???;*.8????;*.9??;*.9???;*.9????;
(I had to use various combinations of ? instead of a single * because the final extension could still be .svn)
More specifically my problem is if I include a pdf in my project and set its build action to content so it gets copied to the server when I publish, is there a way to exclude it from visual studios search and replace feature. Would a post build action be a solution to this? I just don't want anybody to do a global search and replace in my project and mess up the pdfs...
I know I just said a mouth full so let me know if you need clarification. Thanks!!!
If you look in the section headed "Find options" in the find dialog, there is an option titled "Look at these file types". If you qualify the type as *.cs, it will only look in files with that extension. You can also enter multiple file extensions. As long as you don't include pdf files in that filter, it will not apply to them. An alternative is to define a folder set to search in by clicking the ... next to the folder combo. Put your PDF files into a sub-directory of your project and exclude that folder from the folder set.
Additionally, as you have said, not including them in the project and using a post-build event to copy them to the output would also work.
I would qualify though that you are trying to solve a problem that shouldn't really be a problem. The onus is on the developer doing a find and replace to take care in doing so, and to not replace the contents of files they don't intend to change in the first place. Find an replace can be a dangerous tool.