Saturday, June 7, 2014

Windows CMD Batch Scripting Basics


### BASIC SYNTAX and BASIC COMMANDS ### 
@echo off    (creates a window...)
Title Name   ( Create a file name with Name)
color 0a   (change only the BAckgroup to green ... Foregroud Background )
pause      (pause the program...it will show ...press any key to continue... on cmd)
pause >nul  (It won't show the text...)
ping localhost -n  5 > nul 
echo hi      (stdout ... hi) 
echo .    (will give a blank line) 
dir /s     (scan of file in the existing files )
dir C:\windows\system32
cls      (clears the screen...)
msg       (pop up )
start www.google.com     (start website / executable... combining lot of commands) 
:A
goto A
del filename.exe
md 1         (make directory )
time 
shutdown -s -t 60 -c "hi" 

### start and color commands ###
@echo off
title Second Lesson 
start iexplore.exe http//www.google.com     (Allows you to open up a file/website)
start test.txt     (file and batch file has to be in the same directory) 
start filepath:filename    (With Absolute path)

### Goto Command, Markers , and Loops ### 
@echo off
:a
echo hi 
pause
goto :a

### User input ### 
@echo off
title User Input
echo Press 1 to make me say Hi 
echo Press 2 to make me say Bye
set /p car=         (User inputs)
if %car% == 1 goto hi
if %car% == 2 goto bye 
:hi
echo Hi 
pause
exit
:bye
echo Bye
pause
exit

### Variables ### 
@echo off
title Variables
set name= Default
echo What is your name ?
set /p name=
echo Your name is %name%!     (Value of the variable : %name%)

### Directors, Del and Renname ###
@echo off
title Directors, Delete command, and Rename Command
REM Directors     (REM - Comment line - records comment in a batch file)
echo Hello! > hello.txt    (Creates a new file and push the data)
echo Hello! >> bye.txt      (Creates a new file and appends the data)
REM Delete command
del path 
REM rename Command
ren path\file.txt new_file.txt

### Variables in Directors ###
@echo off
title Variables in Directors 
REM Below both are same 
set /p name=Enter the name for your file:   (One liner )
echo Enter the extension for you file:
set /p ext=
REM Sample Program
@echo off
title File maker
set /p name=Enter the name for your file:
set /p extension=Enter the extension for your file:
echo. > %name%.%extension%           (set up a blank line)

### If Not Command ###
@echo off
title If Not Command
set /p protection= Enter password:
if %protection% == Hi goto correct
if not %protection% == Hi goto incorrect 
:correct
echo Correct pass!
pause
exit
:incorrect
echo Incorrect pass!
pause
exit


### Custom Pause and Animation ###
@echo off
title Custom Pause
echo loading
ping localhost -n 2 >nul
cls
echo loading. 
ping localhost -n 2 >nul 

### Environment Variables ###
@echo off
echo %HOMEDRIVE%
echo %OS%
echo %DATE%
echo %TIME%
pause


### CMD COMMENTING ###
@echo off
REM Causes blank lines.
echo. 
echo. 
echo.
echo TEsting
:: This line is also comment 


### Simple Example ###
@echo off
echo set speech = Wscript.CreateObject("SAPI.spVoice") >> "temp.vbs"
set text=Hello, this is a talking batch file.
echo speech.speak "%text%" >> "temp.vbs"
start temp.vbs
pause
del temp.vbs

















No comments: