Windows Commands/Batch All In One


Command
http://ss64.com/nt/
type  filename
Sleep some time
sleep 5
timeout 5
These 2 commands are not available in every windows machine, in practice, we can use ping to cause delay.
ping 1.1.1.1 -n 1 -w 1000 >NUL 2>NUL
start command: run command in a separate window.
call: Calls one batch program from another without stopping the parent batch program. 
find
find [/v] [/c] [/n] [/i] "string" [[Drive:][Path]FileName[...]]
/v(reverse), /c(count), /n(show line number), /i(case-insensitive)
for %f in (*.bat) do find "PROMPT" %f 
dir c:\ /s /b | find "CPU"
sort
/r, /+n, 
more
+n : Displays first file beginning at the line specified by n.
/c, /s
The following commands are accepted at the more prompt:
SPACEBAR Display next page
ENTER Display next line
f Display next file
= Show line number
p n Display next n lines
s n Skip next n lines
q, ?
Widnows Batch

Read until there are 3 lines in file result
@ECHO OFF
:readFileLoop
if exist result (
    set /a "x = 0"
    for /F "tokens=*" %%L in (result) do set /a "x = x + 1"
if "%x%" EQU  "3" (
goto :end
)
) > NUL 2>&1
goto :readFileLoop
@ECHO ON
Using batch files
If statement
CompareOp : 
EQU, NEQ, LSS, LEQ, GTR, GEQ
For for /?
for {%variable|%%variable} in (set) do command [ CommandLineOptions]
Use %variable to carry out for from the command prompt. Use %%variable to carry out the for command within a batch file.
Directories only: for /D
Recursive: for /R
Iterating a range of values: for /L
Iterating and file parsing: for /F
eol=c Specifies an end of line character (just one character).
skip=n Specifies the number of lines to skip at the beginning of the file.
delims=xxx
tokens=x,y,m-n
for /F "eol=; tokens=2,3* delims=," %i in (myfile.txt) do @echo %i %j %k
This command parses each line in Myfile.txt, ignoring lines that begin with a semicolon and passing the second and third token from each line to the FOR body (tokens are delimited by commas or spaces). The body of the FOR statement references %i to get the second token, %j to get the third token, and %k to get all of the remaining tokens.
Using batch parameters
%0-%9
When you use batch parameters in a batch file, %0 is replaced by the batch file name, and %1 through %9 are replaced by the corresponding arguments that you type at the command line. To access arguments beyond %9, you need to use the shift command. 
modifiers: %~f1, %~d1, %~p1, %~n1, %~x1, %~s1, %~a1, %~t1, %~z1
setlocal/endlocal
Use setlocal to change environment variables when you run a batch file. Environment changes made after you run setlocal are local to the batch file. Cmd.exe restores previous settings when it either encounters an endlocal command or reaches the end of the batch file.
Parse Command Args
:parseParams
set key=%~1
set value=%~2
if "%key%" == "" goto :eof
if "%key%" == "-MY_JAVA_OPTIONS" (   
    shift
shift
if "%value%" == "" (
        echo Empty value for -MY_JAVA_OPTIONS
        goto end
    )
SET MY_JAVA_OPTIONS=%value%
    goto parseParams %*
) else (
rem skip some other parameters
shift
    goto parseParams %*
)
goto :eof

:parseArgs
  REM recursive procedure to split off the first two tokens from the input
if "%*" NEQ "" (
for /F "tokens=1,2,* delims== " %%i in ("%*") do call :assignKeyValue %%i %%j & call :parseArgs %%k
)
goto :eof
:assignKeyValue
if /i "%1" EQU "-Xmx" (
SET Xmx=%2
) else if /i "%1" EQU "-Xms" (
    SET Xms=%2

goto :eof
SubString: SET var=%var:~10,5%
http://geekswithblogs.net/SoftwareDoneRight/archive/2010/01/30/useful-dos-batch-functions-substring-and-length.aspx
Save current path and restore it later
set currentPath=%cd%
cd %currentPath%

set MYPATH=%~dp0

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)