Find completely commented files - visual-studio

In a solution with lots of files and projects - how would you find all completely commented files? I assume that every line of code starts with // (EDIT: or is empty) in such files.
I am using VS 2008, C#, ReSharper is available.
I know, normally such files should not exist - that's what a source safe is for ...

To find all files in and under the current directory in which all lines begin with '//':
find . -type f -exec sh -c 'grep -vq "^//" {} || echo {}' \;
Note that this will report empty files.
The argument to grep can easily be expanded to account for whitespace, or generalized to match an arbitrary regex.

There is no way to achieve this with a simple search style with the components you've mentioned. Doing this would require a bit of interpretation on the file but could be done with a fairly simple script.
It sounds like you're looking for files without code though vs. files with all comments. For example if there are 1000 lines where 900 are commented and 100 are blank, it seems to meet your criteria.
The script should be fairly straight forward to write but you would need to look out for the following weird cases
Block comments
if blocks which are always false. For example #if 0
Empty lines

Well, you could write a program (probably a console app) to recursively walk the directory and file tree. Read in all .cs files and check each line to see if its first non-space and non-tab characters are "//". If you wanted to get really fancy, you could count the total lines and the lines with "//" and display the percentages so you could catch files that didn't have absolutely every line commented out. You'll just need to understand a little bit about System.IO to get the files and string functions to look for the characters you are looking for. That should cover it.

This should be close to what you're looking for: http://www.codeproject.com/KB/cs/csharplinecounter.aspx
Look for the method in the project that determines if a line is commented or not, and you can use that to build a count, etc.

Related

Adding multiple files with one command

I tried to give a sample of a list of my titles. Here it is:
'197. Module Introduction.txt' '198. Our Starting Setup.txt' "199. What's So Complex About Forms.txt" '200. Dealing With Form Submission & Getting User Input Values.txt' '201. Adding Basic Validation.txt' '202. Providing Validation Feedback.txt' '203. Handling the "was touched" State.txt' '204. React To Lost Focus.txt' '205. Refactoring & Deriving States.txt' '206. Managing The Overall Form Validity.txt' 'Assignment 5: Time to Practice: Forms.txt' '207. Adding A Custom Input Hook.txt' '208. Re-Using The Custom Hook.txt' '209. A Challenge For You.txt' '210. Applying Our Hook & Knowledge To A New Form.txt' '211. Summary.txt' '212. Bonus: Using useReducer.txt' '213. Module Resources'.txt
All those titles were in a text file named lectures.txt.
I wanted to create a series of titled blank text files in a folder I had already created to receive them. Each text file should have the specific titles as in that example list. Ultimately I would be creating nearly 500 files, but all of them would have very specific meaningful titles.
I tried to follow all the instructions I found on this forum and elsewhere, on a very long search for help. None of them directly addressed my problem.
They all gave answers for creating multiple files with meaningless names. I don't see the utility in creating a bunch of files named 1.txt, etc, or a.txt, or some combination with a leading standard meaningless lead like filename, or sample...
I may be wrong about that lack of usefulness, but at the very least, it's not what I tried to ask and get an answer for.
I already have the meaningful filenames I want, a lot of them. I want to create many empty text files with names that I already have.
The only remotely useful suggestion (for me) that I got was using touch from a linux or bash prompt.
I found that if I wrapped my titles in quotations and separated the titles by a single space I could get the touch command to kind of work. I also discovered that my file titles could not contain any forward slashes ("/"). Nobody explained to me that the titles had to be wrapped in quotes. I figured that out quickly. Nobody told me how to separate the filenames. I experimented until I found a separation that worked.
I tried some experimenting with the {} bracing and the touch command but wound up not being able to figure out how that could help me in my particular case.
Also, I don't know if it has to do with a buffer on the bash or in PowerShell (I tried using both the Git installation in Windows 10 and bash from PowerShell 7.2.1, but I found that only about seven titles in the list would be touched. The rest resulted in a command not found error.
I broke up the list into about seven titles each and ran touch on each of those filenames (my titles) list segments. That way, on that very list sample I display above from my lectures.txt file, I got my empty text files created successfully, even though I had to do it in three touch commands.
I may have confused some of the people who tried to help me by putting all the titles in a text file that I named lectures.txt.
The contents of that file were the titles I wanted for my text files. Thank you so much those who did reach out to me.
I had already found something that might work in bash but I couldn't get it to work. It was roughly on the line of
`touch prefix{1..3}.txt`
This had the same problem that all the other command suggestions I found had as well, like
echo > filename.extension
The same for the apparently most popular
for /l %a in (1 1 10) do type nul > "%a.txt"
which created ten files named 1.txt through 10.txt. That was not the least bit useful to me.
I know my question got voted down as being exceptionally bad, but I'm editing and adding what I found out, so maybe sort of answering my own question.
If you are looking to do what I'm trying to do, and not just create meaningless filenames, I hope you will find some of what I did helpful. Here's a list of suggested solutions of which NONE did I find useful.
https://techpp.com/2021/08/22/create-file-using...
https://www.quora.com/Can-you-create-multiple-f...
https://www.howtogeek.com/725207/how-to-create-...
How to create multiple empty files on cmd(Windows).
Create multiple files with Powershell?.
https://www.laptopmag.com/how-to/create-multipl...
Remember that using space in bash is not recommended, but if you need to...
You can try something like that:
counter=1
for title in $(cat lectures.txt)
do
touch "$counter - $title.txt"
counter=$((counter+1))
done
EDIT
I only added the title on lectures.txt
NEW EDIT
If lectures.txt has 1 title per line you can do this:
while IFS= read -r line || [ -n "$line" ]
do
touch "$line"
done < lectures.txt
Get-Content -Path '.\lectures.txt' | ForEach-Object { New-Item -Path ".\$($_)" }
or using aliases it's: gc '.\titles.txt' | % { ni ".\$($_)" }
from a powershell console at the required folder and enter the command.

GNU split (UNIX command) creating files not matching pattern after reaching "z"

So I was spliting some large files, everything worked properly until a file of 81GB came to scene. The split command seems that made its job, but the last files has a non correlated name. Look at the right bottom of picture.
And I'm using the command like this:
split -b 125M ./2014.txt 2014/2014_
Anyone knows why instead of create the file 2014_za created the 2014_zaaa?
You can only have 676 files named [a-z][a-z], while your command required more.
Here are some options for what split could do:
Crash.
This is the behavior mandated by POSIX, and followed by macOS.
Start writing larger suffixes.
This is a bad choice because after _zz comes _aaa, but now the files will show up in the wrong order in ls and cat * will no longer join them in correct order.
Save the last range, _z, for longer suffixes.
This is a good choice because after _yz comes _zaaa, which has room to grow while still remaining in alphabetical order. This is what GNU does, and the behavior you're seeing.
If you want all the names to be uniform without triggering any of these behaviors, just use a larger suffix length with -a 6 to ensure you have enough room.

Merge two text files

I'm using Windows and Notepad++ to separate file in txt. I have 2 files which is I have to merge it side by side or line by line for my data analysis.
Here is the example:
file1.txt
Abcdefghijk
abcdefghijk
file2.txt
123456
123456
then the output I want is like this:
Abcdefghijk123456
abcdefghijk123456
in the next file or output file. Does anybody here know how to do this?
Your question answered here by TheMadTechnician. Using powershell, you should take both source files (1 and 2) as arrays of lines. Then comes simple cycle, like "merge line x from file1 with line x from file2 as long you have some lines in file1".
Unfortunately its impossible with pure cmd.
#riki.. you could also write a batch program to do this pro grammatically. There should probably be no limit over the number of lines.
It may depend on the number of lines you're having in each files. I suggest to copy paste the same if it is less than 50 lines.
Otherwise,
use some powerful languages like python, c,php etc. And make it run before performing data analysis.
There is a free utility you can download and run on your computer, called txtcollector. I read about it here. I used it because I had a whole folder of files to concatenate. It was a breeze. The only slight imperfection I noticed was that I couldn't paste in the path to the specific folder in the first step (choosing the folder where the files to be concatenated were). However, I could do this when choosing where to save the result.

Figure out the destination of `ln -s` using script

First, here's the script I'm talking about:
https://github.com/Greduan/dotfiles/blob/master/scripts/symlinks.sh
Check line 20. It has the following content
dest = "$HOME/.`basename \"${source%.*}\"`"
Since I took this from another script, I have no idea what it actually does. I'm guessing what it does is if the source file is named vimrc.vim.symlink it'll output .vimrc.vim, correct?
If not correct, could you please explain what it does?
And could you please help me figure out how to make it so that if the file is vimrc.vim.symlink, how can I make it so that I can get .vimrc?
Please check the script so that you can understand what I'm talking about. :)
First of all, your analysis is correct. ${source%.*} removes the .* suffix from source. The rest, $HOME/.$(basename \"...\") takes the basename of source, which removes all path from it, leaving only the file name and is placed after $(HOME)/..
If you want to remove everything after (and including) the first dot, you can use ${source%%.*} (with %% instead of %). This answer gives you examples.

Diff for 3 binary files

I have 3 binary files. Let's call them file1.bin, file2.bin and file3.bin.
file1.bin and file2.bin have some common parts.
file2.bin and file3.bin have some common parts.
I want to find the common parts between file1.bin and file2.bin that are different between file2.bin and file3.bin.
How do you recommend to accomplish that? I have already dumped the binary files to text files using xxd and then did a 3-way diff using vim -d file1.txt file2.txt file3.txt.
However, vim marks a part as changed in all the files even if it has only changed in one file and remains the same in the other two files. I want those special kind of occurrences to be marked differently.
Perhaps you can use the built-in unix diff (I think it is part of OSX), but use the --unchanged-group-format to list the similarities. Do that for file1 and file 2. Then do it for file2 and file3. You can then do a regular diff on the two resulting files.
For an idea of how to get the similarities, have a look at this post.
The tool that I work for (ECMerge) does that. You just have to diff the 3 binary files, it will present equal portions in front of each other, and modified bytes appropriately placed in between. No need to first get an hex dump. You can script in JavaScript to output whatever you like based on the diff results and the bytes in the files (it works also in command line).
Chromium uses bsdiff, then switched to courgette for doing binary diff as explained in their blog here. You might find useful leads from their blog.

Resources