Easy file manipulation with 4 CMD plugins
Manipulating file contents in Batch can be pretty tedious. Very much so if you are not an expert and you need a solution quickly. Without plugins, you would likely need to create complex algorithms yourself in order to sort through the files lines and contents, just to implement basic text manipulation. This plugin set, created by Karanveer Chouhan (Kvc), offers a solution to this problem. Here we will discuss it’s use cases and how to use it yourself.
Demonstrating The Plugins
Inserting a line:
Let’s use an example to demonstrate how powerful this plugin can be. Lets have a text file that has 6 lines of text in them (the number to the left is the position of the text, and not part of the text). I will write an example one below:
1 Alpha Text Line 2 Bravo Text Line 3 Charlie Text Line 4 Delta Text Line 5 Echo Text Line 6 Fox Text Line
Let’s say we need to insert a line of text at line 4. In raw batch, this would probably involve a lot of programming and may not be necessary for your project. With this plugin, it is as simple as calling the plugin (in this case, addline) and specifying the file name, line number and text to insert.
C:\>addline ourtextfile.txt 4 "Replacement Text"
The plugin will insert the line into the text file and you will be left with:
1 Alpha Text Line 2 Bravo Text Line 3 Charlie Text Line 4 Replacement Text 5 Delta Text Line 6 Echo Text Line 7 Fox Text Line
Replacing a line:
As you can see, addline does not replace the content at that line. It instead pushes it up ahead in the file. Now, let’s say we want to replace the content of a line in this file. We can use the replaceline plugin included, just like the above:
C:\>replaceline ourtextfile.txt 2 "This was replaced"
The plugin will replace the line in our text file with the specified content, as seen here:
1 Alpha Text Line 2 This was replaced 3 Charlie Text Lin 4 Replacement Text 5 Delta Text Line 6 Echo Text Line 7 Fox Text Line
Removing a line:
Hopefully, you are beginning to see the usefulness of this plugin. The four plugins have similar syntax, so they are easy to pick up. The third plugin in this set allows you to simply delete a line from the file.
C:\>delline ourtextfile.txt 7
Which results in:
1 Alpha Text Line 2 This was replaced 3 Charlie Text Lin 4 Replacement Text 5 Delta Text Line 6 Echo Text Line
Returning/reading a line:
The final plugin included in this set allows you to read a line from the file. It is a lot easier than methods to do it in raw batch especially if you do not want to sort through the contents of the file. It will display the content of the file to stdout, which can be redirected just like any other output, so you can create complex file logic which would otherwise be difficult in raw CMD.
C:\>readline ourtextfile.txt 4 Replacement Text
Hopefully, this article has demonstrated the use cases of this plugin effectively. It saves a lot of time, and lowers the risk of human error when developing plugins that need file manipulation like what we have demonstrated above. You can download the plugins from our GitHub Page, and you can also suggest bugfixes and errors there. We are always looking for improvement.
I am a programmer and a computer hobbyist in the United States. I make articles about a variety of topics.