Rahul's Website

Tech, coding, security and other random stuff...

Command line arguments on Windows

Aug 032017

The following command shows how command line arguments basically work on Windows:

executable.exe arg1 "this is arg2" \"arg3 arg\"

These are the basics:

  1. Arguments are separated by spaces
  2. Strings inside double quotes are treated as a single argument
  3. 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:

Command Arguments passed
executable.exe \\
executable.exe \"
executable.exe \\"
executable.exe \\\"

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:

>executable.exe \\"
There are 2 arguments:
0 executable.exe
1 \

And now let us try the same thing with some extra text at the end:

>executable.exe \\" hi hi" lol
There are 3 arguments:
0 executable.exe
1 \ hi hi
2 lol

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!

This also explains the last case. The third backslash is escaping the double quotes!

Decoding Percent-Encoded Data from CMD

Jul 302017

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.


percentdecoder.cmd <encoded-string-goes-here>

(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>.

Using diskpart to reformat corrupt drives

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:

  1. Open cmd as an administrator.
  2. Run diskpart.
  3. Type in select disk.
  4. Then enter select disk <yourdisk>.
  5. Type in clean.
  6. Then enter select disk <yourdisk> again.
  7. Finally run the command create partition primary.
  8. Open Windows Explorer, right click on your drive, and format it.

That's it, you should be able to use your drive normally again!

Create and Enter a Directory in Command Prompt

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:

C:\Users\Rahul\Documents\Coding>md mydirectory
C:\Users\Rahul\Documents\Coding>cd mydirectory

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 System32 Folder.

  • Type @md %1 && cd /d %1, and press enter. These commends basically pass on the first command line argument of mdc.cmd to 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.