Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better than MSDOS batch files? [closed]

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

like image 686
Vidar Avatar asked Jun 04 '09 17:06

Vidar


4 Answers

Take a look at PowerShell

like image 68
CoderDennis Avatar answered Oct 23 '22 16:10

CoderDennis


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

like image 32
Jeremy E Avatar answered Oct 23 '22 16:10

Jeremy E


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.

like image 5
gimel Avatar answered Oct 23 '22 16:10

gimel


VB Script in a plain .vbs file.

like image 4
Dan Byström Avatar answered Oct 23 '22 14:10

Dan Byström