Showing posts with label Bat Scripts. Show all posts
Showing posts with label Bat Scripts. Show all posts

Wednesday, November 7, 2012

Reading Text from a text file into an Environment Variable.

Simply two methods Smile:

  1. Set /P MyText=<C:\MyTextFile.txt
  2. For /F %f in (tmp.txt) do Set MyText=%f.(Note: In batch files, use %%f).

Method 1 is quite simple. It will simply read text from MyTextFile.txt and would redirect the Input to MyText environment variable using ‘<’ operator.

Basically ‘Set /P’ is used to get user input while prompting the user. But using redirection operation ‘<’, we can read files into the prompting variable.

For more information about Set command, please see this link Or at command prompt type ‘Set /?’ and you’ll get a quick help.

 

Method 2 is little bit complex due to its longer syntax, but actually it is more versatile as you can do many other things with the Text you read.

For details about For command, at command prompt type For /? or see this link.

Using above mentioned ‘For’ command, your text would be read into %f and after ‘do’ attribute you can issue any command. E.g:

For /F %f in (tmp.txt) do echo %f

For /F %f in (tmp.txt) do echo %f >> C:\MyNewTextFile.txt (This would append value of %f into C:\MyNewTextFile.txt)

Thursday, October 11, 2012

How to generate a GUID at command line in Windows?

Well, like me who works with WIX and have to deal with lots of GUIDs, you might come across a situation where you may need to generate a GUID at command prompt during a procedure. I faced this while working with WIX based patches and unfortunately, like in other WIX templates i.e. Product, Module, PatchCreation doesn’t support Id=”*” where at the place of *, WIX compiler generates a new GUID each time.

So had to search for a command line utility in order to generate GUID randomly. Actually you can program such things even in VBScript and JScript, but I was looking for some built-in thing, to avoid adding new application to our build machine.

And I found UUIDGEN.exe. Yes, I know, Ancient C programmers are very much aware of this, but I only heard about it onceSmile with tongue out

It comes with almost every Visual Studio and with Windows SDK Tool. On my machine, it was present at following locations:
C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\Uuidgen.Exe

C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\Tools\uuidgen.exe

And here is its command line help:

"C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\Tools\uuidgen.exe" /?
Microsoft UUID Generator v1
.01 Copyright (c) Microsoft Corporation. All rights reserved
usage: uuidgen [-xisconvh]
x - Generate sequential
(V1) UUIDs
i - Output UUID in an IDL interface template
s - Output UUID as an initialized C struct
c - Output UUID in upper case
o
<filename> - redirect output to a file, specified immediately after o
n
<number> - Number of UUIDs to generate, specified immediately after n
v - display version information about uuidgen
h
,? - Display command option summary


In older versions of UUIDGEN.exe you might not getting option "-c" i.e. output upper case. So to generate a quick GUID, simply issue:
”C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\Tools\uuidgen.exe”


and you’ll get something like this:
5fae2556-299f-44dc-af2a-9afa3948e8a2 


and if you need Upper case issue same command by adding –c i.e.


“C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\Tools\uuidgen.exe” -c


and you should one in upper case:
2C4E4CD9-4635-42B6-A88A-099E952B95E8 .


 


How to use UUIDGEN in bat/cmd scripts?


Microsoft has provided “For” command which can be used to run any other command, read files  etc etc and then further process their outputs. Here is how you can use For with UUIDGEN to save generated GUID into an environment variable:

for /f %i in ('UUIDGEN') do set aGUID=%i


and this would define an environment variable aGUID containing your generated GUID.


 


 

Monday, October 8, 2012

Using ICacls to grant permission.

ICacls is the updated form of older utility “Cacls” and is used to grant permissions upon files and folder to users from command line interface. I wanted to use it with one of my folders present in C:\ProgramData and I wanted to give Users group full access my folder. Searched on multiple sites and I’ve got a quick syntax:

iCacls %ProgramData%\MyFolder /grant Users:(OI)(CI)F


This is working on my Win 7 x64 and here is the forum post from where I grabbed this:
http://www.vistax64.com/vista-security/125552-how-use-icacls-exe-fully-enable-users-subfolders-files.html#post590382


 


Happy Cacling Smile

Friday, July 27, 2012

How to create Command Alias (Windows)?

While working in Command Prompt and Bat scripts, I came across situations, where I feel the need to define Alias of some commands, because I’m lazy in typing long Words again and again. One such Command is “Robocopy” .. You see it’s not as long, but due to my laziness in typing it again and again, I started looking for creating Alias of Robocopy command, such as RC..
After searching on net,
Doskey is the command built-in to Microsoft Windows is used to create such Aliases and I was bit surprised, because in past I used Doskey to keep command names and then call these back without typing again and again..
Syntax: Doskey Alias=Command (executables)

And this is what I collected from a SuperUser.com’s post:
Defining Alias for dir command as ls (which is actually Unix/Linux command):

doskey ls=dir

'ls' will now do a directory listing just like dir would.
If you want to use arguments with the commands, use this syntax:
doskey db=dir /b $*

Here is another interesting example:
doskey ..=cd ..

And you can easily guess what it will do with CD .. Smile. In addition, you can Alias your own bat/cmd scripts along with parameters:
doskey whereis=c:\winscripts\whereis.cmd $*

More interestingly, you can define your aliases in a file in batch:
   1: ls=dir /ONE $*
   2: cd=cd /d $*
   3: python=python -ic ""
   4: ps=tasklist $*
   5: kill=taskkill /IM $*

And then in your bat/cmd file, let say cmd_autoruns.cmd or at command prompt, simply call it:
doskey /macrofile=c:\bin\aliases

To set your command prompt always loading your bat/cmd file defining your Aliases you can update your registry key either using “Reg” command:
reg add "hkcu\software\microsoft\command processor" /v Autorun /t reg_sz /d c:\bin\cmd_autoruns.cmd

Alternatively, using RegEdit, you can edit your registry like:
   1: REGEDIT4 
   2:  
   3: [HKEY_CURRENT_USER\Software\Microsoft\Command Processor] 
   4: "CompletionChar"=dword:00000009 
   5: “DefaultColor"=dword:00000000 
   6: "EnableExtensions"=dword:00000001 
   7: "PathCompletionChar"=dword:00000009 
   8: "Autorun"="c:\\bin\\cmd_autoruns.cmd" 
And if you wish this registry change for every user (machine level), then use this registry location:
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun

Happy Aliasing Smile with tongue out