2>&1
", as in
"app 2>&1 | head
".TYPE
to get rid of the Unicode(UCS-2)-ness of
the output of REGEDIT /E
.@if exist %~s1\nul (echo dir) else if exist %~s1 (echo file) else echo nosuchIt doesn't work on network drives though. I've had better luck with this version:
@echo off setlocal set FOO=%~1 if not "%FOO:~-1%"=="\" set FOO=%FOO%\. dir /b/ad %FOO% >nul 2>nul if errorlevel 1 (if exist %~1 (echo file) else (echo nosuch)) else echo dir
COLOR
.TITLE
" to set the title of individual
DOS-boxes to distinguish them.
<
and >
by prefixing
them with ^
, as in "echo ^<hello^>
".SET /P
var=prompt
"
to set environment variables to user input. But with both search-and-replace
and slice notation (see below), it starts to become a lot more interesting.
SET /A expression
" or "SET /A var=expression
".
%BLAH:old=new%
")
and slice notation ("%BLAH:~1,5%
"). Actually, it's not quite
slice notation, since the (optional) second parameter is the string
length, not the end position.
As in Python, you can use negative values for the first parameter to start n
characters back from the end of the string. Use "HELP SET
"
for more info.
(This is only true with command extensions enabled, but they are by default, and
there's pretty much never any reason to disable them. Do "HELP
CMD
"/"HELP IF
" to find out all I know about command
extensions.)
CMD /K DOSKEY
" to load DOSKEY. This gives you a
history with the
arrow keys, which is vital, and "DOSKEY d=dir $*
"-style
macros (or "DOSKEY export=set $*
") to make your life easier
(alternatively, do e.g. "echo @dir %* >
%windir%\system32\d.bat
", but that's ugly).COPY
can be used to concatenate files. Just put plus
signs between the source files.You can create one-line reminders for yourself by doing something
like:
'AT 11:30 /interactive cmd /c "echo It's 11:30, go drink fizzy
beverages!&&pause"
'.
@if $%2$==$$ echo usage: alarm TIME MESSAGE @if not $%2$==$$ at %1 /interactive wscript say.vbs %* @atand this in "say.vbs":
s = "" for each arg in WScript.Arguments s = s & arg & " " next MsgBox sThen you can do "
alarm 12:00 Lunch break!
" and get a message
box instead of a dos box interrupting you.
for /F %f in ('dir *.tmp /s/b') do @del %f
.
Don't forget to double the "%
"s if you use it in a batch
file.REG
" on Windows XP or
later (see "REG /?
") or see http://www.RobVanDerWoude.com/ntregistry.html
for an NT-compatible alternative using RegEdit to write temporary files.
It needs some FIND/FINDSTR replacement though.
There. Your life is now better. Now type HELP
and see how
the batch file language has been extended since you last looked.
FINDSTR
is particularly surprising. FOR
now has
many many options. And stick SETLOCAL
at the top of (almost)
all your batch files (or preferably SETLOCAL enableextensions
enabledelayedexpansion
).