Saturday, 6 December 2014

Pass File Content as Parameter

I've learnt a pretty sweet Windows command line trick today by looking into someone's code at work, so I'll share it here.

It's clear that if we want to pass the contents of a file as standard input to a program all we need is:
myProgram.exe < myFile.txt

But what if we want to pass the content of that file as a parameter to the program? For example you have a program that encrypts a password that you pass as parameter, and you have that password already in a file. The solution is assigning the content of that file to an environment variable, and then passing the value of the environment variable as parameter to the program, I mean:

set /P myVar=<myPasswordFile.txt
encrypt.exe %myVar%

In Linux, as explained here, the thing is more simple:

./encrypt `cat myPasswordFile.txt`

No comments:

Post a Comment