Resources To Learn About Batch Files - windows

In my new project I need to use batch files(many of them), but now I need to know about they and where can I read some good tutorials to use they very good ;)
PS: I need to distribute my application for end-users.

Well, learning by doing is probably the best way. There are many pitfalls and weirdnesses in the batch language, making some tasks very much non-fun to do. But I think one should at least stepped into each trap once :-)
References are for example (sometimes with extensive examples for specific usage scenarios):
Rob van der Woude
SS64
Technet
Specific problems and solutions may be found on sites like Rosetta Code but there aren't many (and I still didn't get around cleaning up there; the batch examples are horrible). I maintain a few tricks on my own site as well (currently under maintenance, though; struggling with my syntax highlighter).
Others have mentioned it: If you have the option, then by all means use other technologies. PowerShell is a nice one but not included by default on older Windows systems, including Vista. For many more complex tasks VBScript via WSH is usually a better option as it has a similar installed base and is way more powerful.
Depending on your requirements this may or may not be possible, but take it into consideration if it may be an option.

While I usually don't like MSDN, in this case its documentation on batch files seems fairly decent.

Basically with bat/cmd files you are running DOS commands (with extra programs/features available to Windows) with each command on a new line.
The .cmd extension is prefereable to .bat on older Windows systems as it runs with cmd.exe, which executes faster than the older command.com. On newer systems (XP and above I think) then it doesn't matter if it is .cmd or .bat then they get executed by cmd.exe.
Here's some tips/examples on writing batch files:
http://www.ericphelps.com/batch/
Wikipedia has a list of DOS commands here:
http://en.wikipedia.org/wiki/List_of_DOS_commands
Here's some info on invoking the actual cmd.exe
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/cmd.mspx?mfr=true

You can start with: Command Line = PowerShell / bash / zsh / ...
Batch is very old-fashioned and practically obsolete now.

The one thing I can recommend if you're working with batch scripts in Windows is learn PowerShell. It's a souped up command line that will allow you to interact with any .net object.
Have fun!

Besides everything that was suggested, I can recommend using VBScript (.vbs). To use Powershell, you need to install it (although it might be installed along with Windows Updates). To use VBScript (which is also very powerful) you don't need anything, it's in Windows by default.

On Windows, use Start|Help and Support Center, and search on "Using batch files", and then in the list of topics under "Overviews, Articles and Tutorials" in the Suggested Topics, click on "Batch files". You'll find links to using parameters, filters and redirection, as well as reference links for batch commands like CALL, ECHO, FOR, and so forth.
The one advantage of batch files over PowerShell is that PowerShell may not be installed, while the command processor (CMD.EXE, or the older DOS version COMMAND.COM) will definitely be there.

Related

Finding PowerShell Cmdlets equivalent to GUI actions

I would like to know where I could find good resources/documentation on configuring a new Windows10 installation using Powershell scripts. I know bash but I'm completely new to Powershell.
When I search google, all I can find about automatically configuring Windows relates to Windows Deployment Services. But I don't have and don't want a Windows Server and simply running a few scripts after each installation is sufficient for me.
I found a few scripts that solve some of the things I want to do:
https://github.com/FlatlanderWoman/winCleaner
https://github.com/hahndorf/Set-Privacy
But for everything else, when I look into the TechNet Library I find it very hard to find anything useful. And when I do find something, it looks outdated:
https://technet.microsoft.com/en-us/library/hh852115.aspx
The problem is: I known the GUI-way of configuring everything I want, but I don't know how to find the corresponding commandlets to do the same with Powershell.
Is there some kind of event listener I could use to find the Cmdlets? Or does anyone have some resources/documentation to recommend? Is the TechNet Library really the established way to find these commands?
Thank you.
Unfortunately PowerShell was only really implemented in Windows 7 (yes I know it was available for XP but not preinstalled) and even then it was kind of like an addon rather than part of the core OS. Windows 8 and 10 have further improved functionality but still for the most part do not use it for their own settings and functions as most home users would have no use for it.
However there is nearly always a way to do whatever you need to, I have a script that configures servers from scratch, renaming the server, installing requisite software and features, copying files, configuring VSS, right down to putting the Computer icon on the desktop. You just have to make a list of everything you want to do, then Google each one.
For example: https://www.google.co.uk/search?q=powershell+put+computer+on+desktop - at time of writing the first result is a TechNet script pointing at a registry key. Tidy as necessary, whack into your build script and move on to the next item.
As of yet there's nothing I've found I've been unable to do with PowerShell, but the vast majority of it has not been directly with cmdlets. There's a lot of registry tweaking and command line stuff like msiexec or schtasks, some COM objects and an awkward Type I had to create and use to set the DNS suffix.
Overall I think it's still easier to do all this in PowerShell than any other scripting language and it's more flexible than premade tools, not because it has so much functionality built-in but because it can access .NET and COM which gives you broad access to all the half-baked stuff MS have wedged in over the years.

win cmd.exe in Vim tab

I'm looking for Vim/gVim plugin to use Windows shell (cmd.exe) (executing commands, retrieving output) in separate tab/frame.
There are a lots of such solutions for Linux, but I'm unable to do it in Win...
Unlike Emacs, Vim is an editor, not an environment (thank Gawd :) and it is not (nor will ever be, according to what the developers have been saying for the past 20 years) designed to house a command prompt/shell in one of its buffers.
That being said, there are a couple of plugins that try to imitate that functionality. Some work better, some worse ... check'em out, and see what works best for you:
cmd.vim
Shell.vim
Conque
Just to add, I don't use any of the above - just found them on vim.org, so you're on your own from here on.

Programming a terminal emulator, what's in it?

This is related somewhat to this question about a better shell terminal/gui-interface for cmd.exe
In my quest to find a better shell terminal, the only useful thing I came across was Console2, other alternatives weren't free and generally didn't offer much more than Console2 to make them worth their price.
I can't help but wonder, "how come"? The shell terminal is a very valuable tool to a programmer, yet no one came around to try and do a better job than cmd.exe (except for the guys # console2)??
Surely designing a command line shell terminal emulator can't be such a hard task!
Has anyone tried writing a shell terminal emulator before? What's in it? Any resources out there (say, if I want to program my own shell)?
UPDATE
17/07/2009
I rephrased the question, what I was referring to as a "shell" is actually called a terminal emulator (at least in the linux world). I only realized this recently so I thought I should revisit this question and fix it.
There are plenty of shells around for Windows, bash under Cygwin is the one I use the most, and it's certainly free. I have to be honest here, cmd.exe has come a long way since the batch file processor of MSDOS. It's actually quite powerful, but still not a pimple on the rear end of bash :-).
You should try to write a command line shell, it will be an education for you. It's not that hard to do the basics if all you want is a program launcher.
But, if you want all the power of a real shell, including a full blown programming language, job control, piping, output redirection and (seriously) too many other things to list, we probably won't be hearing from you for a while. Except when you pop up your head to ask us esoteric questions about how shells should do this or that.
What about PowerShell from Microsoft? AFAIK it's free, and gives you C# power right in the shell, and tons of other features. But, it DOES require the NET Framework...
I won't endorse it yet, as I haven't really used it, but it's on my list to do...
My bets are on Powershell for the future if you are a Windows guy.
PowerShell will be installed by default on Windows Server 08 R2 (WS08R2) and Windows 7 according to MS.
I have used Powershell myself and found it to be very useful, and if you are familiar with .NET then its all the more easier.
Download a two page reference document here, this is all you will need to get started.
I mean the gui-interface part that displays the text,
Ah! you mean what we call a "pseudo-tty" or a "terminal emulator" in unix. In windows, I guess it is called a "console host". I do not use windows, but I heard that console2 is very good.
Seems there are others:
http://www.powershellanalyzer.com/
http://powershellide.com/
http://www.codeplex.com/PoshConsole
and, of course, for real programmers, you have the shell mode of (x)emacs :-)
Erm... what about all the ports of Unix command shells to Windows?
Just go with cygwin and log on using PuTTY. So much better than the standard console. :)
Also, Python (or perl for that matter) can replace the shell entirely.
You might want to try Take Command Console, formerly 4NT, formerly 4DOS.
Quick summary of points you might care about:
Command line editing with filename completion, history, and cut & paste
Integrate PERL, Ruby, REXX, Python, and any Active Scripting language
Fast - 20-200% faster output display than the standard Windows console
Integrated file explorer - examine directories, drag and drop files
Upwardly compatible with CMD.EXE with literally thousands of additions
Not free (except trial version) and not open source. I haven't tried it myself, but I was a 4DOS junkie back in the day, before Cygwin.
If you need handy terminal - give a try to ConEmu (I'm the author). It is a Windows console window enhancement (local terminal emulator), which presents multiple consoles and simple GUI applications as one customizable tabbed GUI window with dozens of features.
Another answer and comments

What tools and languages are available for windows shell scripting?

I want to know what are the options to do some scripting jobs in windows platform.
I need functionality like file manipulations, registry editing etc. Can files be edited using scripting tools?
What other functionality does windows scripting tools offer?
Can everything that can be done using the Windows GUI be done using a scripting language?
I think Windows PowerShell from Microsoft is the current favourite for this sort of thing.
It might be worth looking at the prerelease of version 2.0. A lot of stuff has changed:
http://blogs.msdn.com/powershell/archive/2007/11/06/what-s-new-in-ctp-of-powershell-2-0.aspx
How about installing a windows version of Python, Perl or your favorite language? These should offer all the functionality you need.
Batch files are the most portable, but doing complicated things can get hard (very hard).
Powershell is incredibly - um - powerful, but the installed domain at the moment is only slightly more than those people who like using powershell and servers they administer. If you control the machines you're scripting on and can mandate that powershell is installed, powershell is the way to go. Otherwise, batch files are the best way.
Powershell lets you do anything which can be done, but some things will be harder than others :)
(Seriously, if you want to control a windows GUI app from a script, you're going to have a lot of pain unless the app supports scripting itself, or you want to start posting messages to dialog controls and screen scraping the dialog to check for success.)
I would recommend "Take Command" (JPSoft), which is more like "cmd.exe" than the PowerShell. We use here at ESSS for years.
Windows Scripting Languages
Also take a look at PowerShell
CScript ? I remember seeing something like that.
Powershell is nice, but it's an extra thing you have to install. Not standard in almost any windows installation. So, if it's just for your own use, then powershell should be fine. If you need the scripts to run on the computers of the general population, for instance, as part of a piece of software you are producing, it may not be the best option.
If you are ok with an extra thing you have to install, you might want to check out cygwin. This allows you to have a full Linux bash command line and all the associated tools at your disposal.
If you need something included in a default windows install. There's the windows command line (cmd.exe). Which has some functionality, but is very deficient compared to what's available in Linux. The other, probably worse problem, is that there isn't much out there in the way of documentation.
You might also be interested in VB Script (flame away). VB Script should work on almost any recent standard windows install, and is a lot more functional than the command line.
I have cygwin installed, so I can run bash shell scripts for my automatization needs. Besides, when I need stuff running moreless natively on Windows, I use a combination of batch + jscript (runs on cmdline if you have Visual Studio.Net installed, just call "cscript XXX.js").
Scripting is a blast.
Personally I like to write some evil little batch files. You can find a command line program to do just about anything. I prefer Batch files mostly because they are portable from one machine to another with at most a zip with a few unix tools (SSED, GREP, GAWK). there is a commandline REG.Exe that can even do Registry changes and reads. You can parse output from commands with a "FOR /f" loop.
PowerShell does have more... err.. Power (2nd post I wrote that in, but I can't help it.)
If you want to look at windows automation check out AutoHotKey.
What are you trying to automate? That may help us narrow down what would be helpfull.
Josh
EDIT: for the record I typed that at the same time as #jameso If someone at work hadn't asked me a question, I may have posted before him. I did get a bit of a shiver at the similarity of the post though....
Powershell can do what you need.
file manipulations
This SO post answers how you can replace a string in your text file. Pasting it here for easy reference:
(Get-Content c:\temp\test.txt).replace('[MYID]', 'MyValue') | Set-Content c:\temp\test.txt
There are other things that you can do, such as copying files and folders. You can find out more on the Windows Powershell Documentation
registry editing
This can be easily done using Powershell. Here's a sample code from Microsoft Dev Blogs:
Set-ItemProperty -Path HKCU:\Software\hsg -Name newproperty -Value anewvalue
Yesterday I could have repaired this for you ;)
What all are the tools/languages for
windows shell scripting?
Would read better as
What tools and languages are available
for windows shell scripting?

Easy installation method for windows/ Batch Reference needed?

I have a bunch of files that I need to be able to transport and install quickly. My current method for doing so is moving a flash drive with a readme file of where stuff goes whenever I need to move stuff, which is rather inelegant and cumbersome.
My idea for a solution would be to write up a quick script to move files around that I could just click on. I've done some bash scripting before but batch scripting is a little odd to me. Does anyone have a good online reference guide I could use?
An alternative soulution I could accept would be a program that makes an installer for you, though I'm a bit against that as I would lose a lot of control. However, I'd be alright with it if it was extremely simple,
Sounds like robocopy tool is exactly what you need.
Very powerful replication command-line tool.
MS TechNet reference,
Wikipedia article about robocopy,
Full command switch guide,
Batch scripting guide.
I like to use VBscript for this kind of thing. The VBS engine is on every recent windows machine and the language is a little more like real programming than a batch script.
Also, if your installer grows to require WMI functions too, this becomes a piece of cake.

Resources