views:

616

answers:

14

Is there something better than using MSDOS in a bat file to run commmand line operations and copy files around.

I am running into the old chestnut "gotchas" with long file names etc - and for some reason the bat file wont pause - when I insert PAUSE in my script after running a command - it's just annoying.

Whats better out there?

Cheers folks.

BTW - Just looked at Powershell and looks like the network/sys admin has blocked Powershell on our PCs (nice).

+4  A: 

VB Script in a plain .vbs file.

danbystrom
Ditto on the VBS - I have a nice batch of VBS scripts for my routine maintenance type stuff. I'm sure I'll eventually have to move to PowerShell, so I up-voted both. :)
AnonJr
+19  A: 

Take a look at PowerShell

Dennis Palmer
LOL 6 powershell responses in 30s
Ryan
A: 

see - Windows Powershell (formerly monad)

AB Kolan
A: 

Scripting PowerShell is supposed to be quite nice. I have never done it, though, and you if you intend to distribute the script, the script users will need PowerShell as well.

PeterAllenWebb
A: 

Microsoft's new-hotness command line and scripting language is called PowerShell. It fixes many of the gotchas of batch files.

Ryan
A: 

Use ZTreeWin, it's a powerful Win32 text-mode file/directory manager and can be run without having to be installed.

Rob Kam
+2  A: 

Install cygwin and use bash scripts, or install perl and use perl scripts, or install ant and use...... hmmm... I forget what you use there. Oh wait... ant scripts

Jherico
A: 

Also out of date is 4DOS. Mind the licensing.

kmarsh
+3  A: 

I routinely install bash and friends on every Windows box I use. A lot of folks use cygwin for this, but I far prefer MinGW.

T.E.D.
+5  A: 

There are a few rules of thumb when working with bat files.

  • Use setlocal endlocal to preserve your enviroment variables outside the script
  • Use double quotes whenever you work with files to allow files with spaces in the name
  • Use pushd/popd instead of cd to move between directories also works with UNC paths
  • If you run another bat file use the call keyword before it or your script will transfer control the new bat file and never return to the original.

Example: quicksql.bat

@echo off
setlocal

if "%1"=="" goto USAGE
set server=%1
if "%2"=="" goto USAGE
set database=%2
if "%3"=="" goto USAGE
set script=%3

sqlcmd.exe -S %server% -d %database% -i "%script%"
goto EOF

:USAGE

echo %0 server database script

:EOF
endlocal

Jeremy E
A: 

Windows Scripting Host.

  • You can use a variety of languages to implement such as VBScript, JScript, or Python (I think I've even seen Perl).
  • You get full access to the language built-in libraries.
  • You get richer API's than with the plain-ole-shell.
  • WSH is included with all shipping versions of Windows.
  • You can mix-and-match languages, reuse blocks, add new libraries, etc.
James Schek
+5  A: 

Actually, answers referring to VBScript really mean Windows Scripting Host:

WSH is a language-independent scripting host for 32-bit Windows platforms. Microsoft provides both Microsoft Visual Basic Script and Java Script scripting engines with WSH. It serves as a controller of ActiveX scripting engines, just as Microsoft Internet Explorer does. Because the scripting host is not a full Internet browser, it has a smaller memory footprint than Internet Explorer; therefore, WSH is appropriate for performing simple, quick tasks. Scripts can be run directly from the desktop by double-clicking a script file, or from a command prompt. WSH provides a low-memory scripting host that is ideal for non-interactive scripting needs such as logon scripting, administrative scripting, and so on. WSH can be run from either the protected-mode Windows-based host (Wscript.exe), or the real-mode command shell-based host (Cscript.exe).

Any windows language (besides vbs and js) that has access to good old COM (ActiveX) can use the same scripting objects. Python is one example, and .NET with P-Invoke is another.

The Script Center Script Repository on technet contains many examples of WSH usage in system administration, most in VBS.

gimel
A: 

Take Command is an option. I use it and love it:

http://www.jpsoft.com/index.html

Provides a Real Windows Scripting Language

164 Built-in Commands
245 Functions
159 System Variables
Mature well tested code
Upwardly compatible with CMD.EXE with literally thousands of additions

William Leara
A: 

You might consider Tcl. You can get a single file executable named tclkit, and associate it with .tcl files. Tcl has very robust file handling commands, and you also have the option of displaying native windows to display progress, add file choosers and the like.

Tcl isn't everyone's cup of tea, but it is a very powerful scripting tool. And with tclkits, deployment is trivial since it's just a smallish single file executable.

Bryan Oakley