The following command shows how command line arguments basically work on Windows:
executable.exe arg1 "this is arg2" \"arg3 arg\"
These are the basics:
- Arguments are separated by spaces
- Strings inside double quotes are treated as a single argument
- To include a literal double quote, escape it using a backslash
Seems easy enough, right? Well, this will cover you for most cases, except...
Try the following out:
The last two cases don't make a lot of sense... So what's going on here?
Turns out that backslashes can be used to escape backslashes themselves, but only when they are followed by double quotes... Confusing? Let me explain.
I've created a simple executable which tells me the command line arguments using code from here. Now let us try the second-to-last case again:
There are 2 arguments:
And now let us try the same thing with some extra text at the end:
>executable.exe \\" hi hi" lol
There are 3 arguments:
1 \ hi hi
What do we see? The first backslash is escaping the second backslash, which is why the second backslash isn't escaping the double quotes, which is why the text between the quotes is getting treated as a single argument!Jul 302017
This also explains the last case. The third backslash is escaping the double quotes!
A couple of months ago I needed to decode percent-encoded POST form data in a batch file. I could find no existing scripts or utilities to do so, so I wrote my own.
You can find the Github repository here: https://github.com/rahuldottech/percent-decoder
It is written in pure Windows Batch, and performs surprisingly well.
Like all my other code, it is licensed under the MIT license.
(it works even if you rename the file)
If you find a bug, open an Issue on Github, leave a comment bellow or send me an email at <code@mydomain>.Jun 292017
Original posted on 18th May 2017 at http://rahul2001.com/weblog/2017/05/using-diskpart-to-reformat-corrupt-drives
Okay, so. Over the years I've come across several corrupt drives. And in my experience, if it is at all possible to make them usable again, this method will do it for you:
cmd as an administrator.
- Type in
- Then enter
select disk <yourdisk>.
- Type in
- Then enter
select disk <yourdisk> again.
- Finally run the command
create partition primary.
- Open Windows Explorer, right click on your drive, and format it.
That's it, you should be able to use your drive normally again!Jun 292017
Original posted on 5th June 2016 at http://rahul2001.com/weblog/2016/06/create-and-enter-a-directory-in-command-prompt.
Often while using command prompt, I want to create a directory and enter it. To do this I need to type in the following:
This is such basic stuff, you would think that Windows would have functionality to do this with one command... But does it? No.
Which is why I've come up with the ultimate solution... Which is just common sense, really.
Open up command prompt and type
copy con mdc.cmd.
This let's you create and edit the file 'mdc.cmd', which is located by default in the
@md %1 && cd /d %1, and press enter. These commends basically pass on the first command line argument of
md, and then chose that directory with
cd /d. I like to have the
/d switch because it allows us to switch the current drive as well, but it is totally up to you.
Press Ctrl+Z and hit enter to stop editing the file.
Now everytime you want to create and enter a directory, just enter
mdc mydirectory and Command Prompt will automatically enter
mydirectory after creating it.