Skip to content

Latest commit

 

History

History
207 lines (163 loc) · 4.05 KB

DOS-cheat-sheet.mediawiki

File metadata and controls

207 lines (163 loc) · 4.05 KB

Table of Contents

DOS and UNIX

Similar (not necessarily equivalent!) programs in DOS and UNIX.

DOS UNIX
rmdir /s/q rm -fr
dir /s ls -R
dir /a ls -a
doskey /history history
echo %cd% echo $PWD
findstr grep
type nul > emptyfile > emptyfile
cmd > nul 2>&1 cmd > /dev/null 2>&1
fc diff
net statistics server uptime
cd %0\.. cd $(dirname "$0")
tasklist ps -xa
taskkill /pid 8168 kill 8168
taskkill /pid 8168 /f kill -9 8168
... | find /c /v "" ... | wc -l
... | find /c "java" ... | grep java | wc -l

If you want to use UNIX commands in DOS, get coreutils!

http://sourceforge.net/projects/gnuwin32/files/coreutils/

Useful DOS commands

  • title - set the title of a cmd window
  • color - set the colors of a cmd window
  • tasklist /FI "IMAGENAME eq yourprocname*"
  • `net use` - find what a network drive is mapped to (find UNC path of a drive letter)

Enable filename completion in Windows 2000

  1. Run regedit.
  2. Navigate to HKEY_CURRENT_USER\Software\Microsoft\Command Processor.
  3. Make a DWORD value EnableExtensions, equal to 1.
  4. Make a DWORD value CompletionChar, equal to 9.

Other tips

1. You can drag files and directories from an explorer window to a DOS terminal window to insert the path (correctly escaped).

2. To delete a hidden file:

 attrib -h the_file
 del the_file

  • Note: luckily, tab completion works with hidden files too.
3. Various net statistics

 net statistics server
 net statistics workstation

4. Compare two files

 fc f1 f2 >nul
 if errorlevel 1 echo Different

5. Map network drive

 net use Z: \\server\folder

6. Get basename without extension

 set basename_without_ext=%~n0

7. Absolute path of current script

 echo %0

Shell script template (sample)

 SETLOCAL
 
 if not exist config.cmd (
     echo The file config.cmd must exist in the current directory.
     echo Create config.cmd from config.cmd.sample and run again.
     pause
     exit /b 1
 )
 call config.cmd
 
 rem Validate configuration
 rem
 if not defined sqlcmd_exe call:undefinedvar sqlcmd_exe & goto:eof
 if not exist %sqlcmd_exe% call:missingexe %sqlcmd_exe% & goto:eof
 if not defined dbserver call:undefinedvar dbserver & goto:eof
 if not defined dbuser call:undefinedvar dbuser & goto:eof
 if not defined dbpass call:undefinedvar dbpass & goto:eof
 if not defined dbname call:undefinedvar dbname & goto:eof
 
 set cmd=%sqlcmd_exe% -S%dbserver% -U%dbuser% -P%dbpass% -d%dbname% %sqlcmd_exe_args% %*
 
 set start_datetime=%date% %time%
 echo.
 echo *** %0: %start_datetime%
 echo *** Command: %cmd%
 echo.
 %cmd%
 set exitcode=%errorlevel%
 echo.
 echo *** Command exit code = %exitcode%
 echo *** Command started on %start_datetime%
 echo *** Command completed on %date% %time%
 echo.
 
 if defined sqlcmd_pause pause
 
 exit /b %exitcode%
 
 goto:eof
 
 :missingexe
 
 echo Error: executable does not exist: %1
 exit /b 1
 
 :undefinedvar
 
 echo Error: variable is not defined: %1
 exit /b 1
 
 goto:eof
 
 rem eof
 

Function template

 :myDosFunc         -- function description here
 ::                 -- %~1: argument description here
 SETLOCAL
 REM.--function body here
 set LocalVar1=...
 set LocalVar2=...
 (ENDLOCAL & REM -- RETURN VALUES
     IF "%~1" NEQ "" SET %~1=%LocalVar1%
     IF "%~2" NEQ "" SET %~2=%LocalVar2%
 )
 GOTO:EOF

Call it with:

 call:myDosFunc %var1% %var2%

Weirdness

Setting variables in a FOR loop

The code below does NOT work!

 FOR %%A IN (c:\test\*) do (
   set value=something
   echo %value%
 )

This works!

 SETLOCAL ENABLEDELAYEDEXPANSION
 FOR %%A IN (c:\test\*) do (
   set value=something
   echo !value!
 )

DOS links

  1. Windows cheat sheet
  2. An A-Z Index of the Windows XP command line
  3. http://www.vbsedit.com/
  4. http://www.emeditor.com/
  5. http://sourceforge.net/projects/gnuwin32/files/coreutils/