Friday, 31 July 2020

2 - Batch script programming 2020 July

Page 02 

------------------

How to open Multiple Application:-

 Syntax  
 
 start

Description:-

To run specified Programme inside the batch file 
eg. Notepad.exe or word.exe or photoshop.exe 
or all executable files.

Examples:-

notepad.exe
google.com
gmail.com
chrome.exe open all windows in one shot

Sample programme 1:-

@echo off

start c:\main\notepad.exe
start c:\main\google.com
start c:\main\gmail.com
start c:\main\chrome.exe
start c:\main\ieexplore.exe

:: Opening all programmes in one shot

exit
------------------------------
How to open Multiple APP by typing first letter of APP:-


 Syntax
 
  %1  to  %9   
 
Description:-

How to display variables

Example 1:-

input number between 1 to 9 and display same numbers

Sample programme 1:-

@echo off

::UPTO 9 COMMAND LINE ARGUMENT CAN BE GIVEN

if "%1"=="" goto end1
if "%2"=="" goto end1
if "%3"=="" goto end1
if "%4"=="" goto end1
if "%5"=="" goto end1
if "%6"=="" goto end1
if "%7"=="" goto end1
if "%8"=="" goto end1
if "%9"=="" goto end1
echo %1
echo  %2
echo  %3
echo  %4
echo  %5
echo  %6
echo  %7
echo  %8
echo  %9
:END1


How to run the programme: c:\bat\test2.bat 1 2 3 4 5 6 7 8 9 
programme will display 1 2 3 4 5 6 7 8 9 


Example 2:-

How to open Multiple APP by typing first letter of APP:-

To run specified Programme inside the batch file 
eg. Notepad.exe or word.exe or photoshop.exe 
or all executable files.

Open following in batch file

::@echo off
:: file name test3.bat 

if  "%1"=="n" goto prg1
if  "%1"=="w" goto prg2
if  "%1"=="ca" goto prg3
if  "%1"=="p" goto prg4

:prg1
 start c:\windows\system32\Notepad.exe
goto end10

:prg2
 start C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories\wordpad.exe
goto end10

:prg3
 start C:\ProgramData\Microsoft\Windows\"Start Menu"\Programs\Accessories\calc.exe
goto end10

:prg4
 start C:\ProgramData\Microsoft\Windows\"Start Menu"\Programs\Accessories\paint.exe
goto end10

:end10
@echo Please type    n   or    w    or    ca    or    p


NOTE;- Keep correct path of programme file.

To open notepad type n

eg. c:\windows\system32\test3.bat n

To open wordpad type w

eg. c:\windows\system32\test3.bat w

------------------------------

How to add New Path to Existing windows Path:-

 Syntax   
 
 set  

Description:-

set patth=%path%;c:\bat;

Add    C:\bat    directory to existing path, so that, exe file, or com file, or  bat file can run anywhere.
Type this command in dos prompt.

Set command used to assigned variables.

---------------------------

How to Hide files using attrib:-


 Syntax   
 
 attrib

Description:-

Displays, sets, or removes attributes assigned to files or directory

eg. hiden files, protected file, read only files

Examples:-

-r    removes read only files    +r   Put file read only

-h    removed hide  only files    +h  Hide the files

Sample syntax:-

Fore more parameters type    attrib/?

attrib  *.*     -r

attrib    *.*  +r  

 Syntax 
  
     for %%f

Description:-

By using this Syntax we can Open all text files automaticaly one by one
 through notepad.exe

Examples 1:-

for  %%f in (*.txt) do notepad.exe %%f

Sample programme:-

@echo off

for  %%f in (*.txt) do notepad.exe %%f

:end

Example 2

:: how to copy 10 or more directory files to another directory:

copy six different directory 

c:\main\pti , c:\main\wld, c:\main\gen, c:\main\bus, c:\main\spt,  c:\main\uni, 

files to  c:\allfiles\pti, c:\allfiles\wld   etc etc 
 
Here %%K variable to directory pti wld gen bus spt uni

Sample Programme:- 

@echo off

::goto six different directory copy all file to c:\main\six different directory
for %%K in (PTI WLD GEN BUS SPT UNI ) do    copy     C:\main\%%K\*.*   to     c:\allfiles\%%k

::goto six different directory xcopy all file to c:\main\six different directory
for %%k in (PTI WLD GEN BUS SPT UNI ) do    y|xcopy    C:\main\%%k\*.*    to      c:\allfiles\%%k
:: y|xcopy   here pipe sign given for do not prompt if file overwrite.

::goto six different directory  move all file to c:\main\six different directory
for %%k in (PTI WLD GEN BUS SPT UNI ) do    move     C:\main\%%k\*.*   to      c:\allfiles\%%k

::goto all six different directoy delete List*.* file
for %%K in (PTI WLD GEN BUS SPT UNI ) do    del        C:\main \%%K\list.* 

:: copy  or move or xcopy from directory c:\day  to  f:\  
:: total 7 directory are there in c:\day directory, one by one go to that directory, and copy all files
to f: drive.
:end

Example:- 

 The above batch file is equal to below batch file. Manualy copying 
one directory to another directory.
copy  c:\main\pti\*.*  to   f:\main\pti   

---------------------------

How to know your system information:-

 Syntax 
  
  systeminfo    

Description:-

Displays detailed configuration information about a your computer and
 its working operating system , including os-configuration,
 security information, product ID, and hardware properties
 (such as RAM, disk space, and network cards).

Examples 1:-

To view configuration information for a computer named Srvmain, type: systeminfo /s srvmain To remotely view configuration information for a computer named Srvmain2 that is located on the Maindom domain, type

 systeminfo /s    this will give information about computer

 systeminfo /s typecomputername /u domainname-with path use \ slash

To remotely view configuration information (in list format) for a computer named Srvmain2 that is located on the Maindom domain, type:

Sample programme:-

@echo off
:: store system information fo test.txt

systeminfo   >>    c:\main\test.txt

notepad.exe c:\main\test.txt

:: put the output display to test.txt and open  notepad automaticaly.

:end
More Parameters and detail description goto c:\windows\system32\systeminfo/?
---------------------------

How to scan system files and replace correct one:-

 Syntax 
  
  sfc   

Description:-
 
Importent dos commands -- System File checker.......
Scans and verifies the integrity of all protected system files and replaces incorrect versions files with correct versions files.

Examples 1:-

System File checker.......
sfc [/scannow] [/verifyonly] [/scanfile=<file>] [/verifyfile=<file>] 
[/offwindir=<offline windows directory> /offbootdir=<offline boot directory>]

Specified full path and filename before typing command:-
sfc /scannow ( Scans the integrity of all protected system files and repairs files with problems when possible.)
sfc /verifyonly Scans integrity of all protected system files. No repair operation is performed.
sfc /scanfile Scans integrity of the specified file and repairs the file if problems are detected, when possible.
sfc /verifyfile verifies the integrity of the specified file. No repair operation is performed.
sfc /offwindir Specifies the location of the offline windows directory, for offline repair.
sfc /offbootdir Specifies the location of the offline boot directory for offline

Sample programme for System File checker.......:-

type the individual command in dos prompt
---------------------------

How to run programmes at specific time and close programmes at specific time:-

 Syntax 
  
  schtasks    

Description:-

Schedules commands and programs to run periodically or  at a specific time. 
Adds and removes tasks from the schedule.

Schedule Task used to RUN  a programme at perticular given time  time.
or
Schedule task can be used for remainder by creating message box 

I used schedule task for remainder more than 20 programmes to run automaticaly and close programmes automaticaly and 20 remainder alert messages .

Examples 1:-

Schedules commands and programs to run periodically at a specific time. 

 How to make schedule task, external Link given below:-



Task Scheduler
https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-task-scheduler


Task Scheduler for developers
https://docs.microsoft.com/en-us/windows/win32/taskschd/task-scheduler-start-page

You can use the Task Scheduler to execute tasks such as starting an application, sending an email message, or showing a message box. Tasks can be scheduled to execute in response to these events, or triggers.
  • When a specific system event occurs.
  • At a specific time.
  • At a specific time on a daily schedule.
  • At a specific time on a weekly schedule.
  • At a specific time on a monthly schedule.
  • At a specific time on a monthly day-of-week schedule.
  • When the computer enters an idle state.
  • When the task is registered.
  • When the system is booted.
  • When a user logs on.
  • When a Terminal Server session changes state.

------------------
How to check performance of monitor:-

Sample Programme:-

@echo off
:: Run the bat file

C:\Windows\System32\Eventvwr.exe

 C:\Windows\System32\Perfmon.exe

:end

---------

How to run Menu to choose programme

 Syntax 
  
  choice,exe   

NOTE : Menu batch script example programme created by me. 

Description:-

@echo off
:: Give file name menu1.bat
:: Menu drive batch file we have to select number 1 to 9 to run the programme.
echo 1 - google.com
echo 2 - Notepad.exe
echo 3 - calculator.exe
echo 4 - paint.exe
echo 5 - cmd.exe
echo 6 - time.exe
echo 7 - date.exe
echo 8 - xcopy.exe
echo 9 - wordpad.exe

CHOICE.EXE /C123456789/N" Type number between 1 TO 9: " 
  
:: following are 1.bat, 2.bat 3.bat etc
IF errorlevel=9 FOR %%c in (9.bat.BAT GOTO:loop) do %%c 
  
IF errorlevel=8 FOR %%c in (8.bat  GOTO:loop) do %%c 
  
IF errorlevel=7 FOR %%c in (7.BAT GOTO:loop) do %%c 
  
IF errorlevel=6 FOR %%c in (6.BAT GOTO:loop) do %%c 
  
IF errorlevel=5 FOR %%c in (5.BAT GOTO:loop) do %%c 
  
IF errorlevel=4 FOR %%c in (4.BAT GOTO:loop) do %%c 
  
IF errorlevel=3 FOR %%c in (3-BAT GOTO:loop) do %%c 
  
IF errorlevel=2 FOR %%c in (2.BAT GOTO:loop) do %%c 
  
IF errorlevel=1 FOR %%c in (1.bat GOTO:loop) do %%c 

:end

NOTE: keep all following files in c:\bat\ to run this batch file
name menu1.bat, choice.exe, plus batch files 1.bat, 2.bat, 3.bat, 4.bat, 5.bat, 6.bat, 7.bat ,8.bat ,9.bat .

Here I given two example  batchfiles.

@echo off
::give file name for this batch file name 1.bat
:: 1 - open ping google.com

ping google.com

call c:\bat\menu1.bat
:end

@echo off
::give file name for this batch file name 2.bat
:: 1 - open ping google.com

notepad.exe

call c:\bat\menu1.bat
:end

:: like creaat batch file for calculator.exe, paint.exe etc

More Parameters and detail description goto c:\windows\system32\choice.exe /?
---------

How to trace remote computer using IP address
or web URL:-

 Syntax 
  
  tracert  /d

Description:-

Determines the path taken to a destination by sending Internet Control Message Protocol (ICMP) echo Request  

What is the use of trace root :-

How to Use the Traceroute Command. Traceroute is a command which can show you the path a packet of information takes from your computer to one you specify. It will list all the routers it passes through until it reaches its destination, or fails to and is discarded.

d/     =   Prevents tracert from attempting to resolve the IP addresses of intermediate routers to their names. This can speed up the display of tracert results.

For more detail parameters  c:\windows\system,32\tracert/? 

Example:-

To trace the path to the host named corp7.microsoft.com, type:
tracert corp7.microsoft.com

Sample programmes:-

If you want to tracert for multiple remote computer then you can put it in
batch file.

Run this syntax in Dos command 

tracert google.com  

or   

tracert  typeIP address here

google IP address or URL is 8.8.8.8.8.8.

Is it legal to ping website:- 1 ping is absolutely legal. 100,000 at once and you might get in trouble.

Note:- It will list all the routers (from your computer to remote computer,)
it passes through until it reaches its destination, 

tracert /j 10.12.0.1 10.29.3.1 10.1.44.1 corp7.microsoft.com


---------
How to change color of Monitor

 Syntax 
  
 color 


Description:-

Changes the foreground and background colors in the Command Prompt 
use without parameter, restore default color.

Example:-

use color with 0 to 7 parameter 
0 is Black
Sample programmes:-

c:\main\color  04    
 
type the command in commond prompt prompt changes to light Red.

---------
How to pause batfile?

 Syntax 
  
  timeout 


Description:-

Pauses the batch file  for the specified number of seconds

Example:-

timeout 120
pause for 120 seconds

Sample programmes:-

@echo off
:start

timeout 180

xcopy c:\main\*.* c|main\tmn
:: backup all files and pause running batchfile180 seconds
goto start

---------------------------
How to open text file without using notepad


 Syntax 
  
  type | more 

Description:-

Controling screen display scrolling  page-by-page

Example:-

type | more   test.txt  

type (use pipe key above slash) test.txt
  

---------------------------
How to send message without using app.

 Syntax 
  
  msg 

Description:-

Send Message to user

Example:-

MSG {username | sessionname | sessionid | @filename | *}
[/SERVER:servername] [/TIME:seconds] [/V] [/W] [message]

username ---- Identifies the specified username.
sessionname ---- The name of the session.
sessionid ---- The ID of the session.
@filename ---- Identifies a file containing a list of usernames,
sessionnames, and sessionids to send the message to.
* ------Send message to all sessions on specified server.
/SERVER:servername ------server to contact (default is current).
/TIME:seconds ------Time delay to wait for receiver to acknowledge msg.
/V Display ------information about actions being performed.
/W ------Wait for response from user, useful with /V.
message------ Message to send. If none specified, prompts for it
or reads from stdin.


more parameter type msg/?
---------------------------

HOW TO BACKUP YOUR ALL DIRECTORY IN YOUR COMPUTER AND FILE NAMES:-

 Syntax 
  
  tree  /f

Description:-

Displays names of files in a directory, subdirectory files 

Example:-

tree  /f    >   c:\main\files.txt

/f = list all files in directory and sub directory.

open files.txt in notepad and check directory tree.

Note. Always check file path and run the programme

Sample Programme:-

@echo off

:: useful to take backup how many directory present in your computer 
:: useful to take number of files names present in your computer

c:\bat\ tree /f > test.txt :: to creat file and store
or
c:\bat\ tree /f >> test.txt

::display all directory and filenames after c:\bat directory
exit
---------------------------
How to display active TCP connection.

 Syntax
 
 netstat   

Description:-

Displays active TCP connections, Displays protocol statistics. Used without parameters, this command displays active TCP connection

Important:This command is available only if the Internet Protocol (TCP/IP) protocol is installed as a component in the properties of a network adapter in Network Connections.

Examples:-

netstat   /a     
 
displays all TCP corrections

More Parameters and detail description goto c:\windows\system32\netstat/?

---------------------------
How to use -- if exist -- command

 Syntax
 
 if exist


Discription:-

Displays windows  output one screen at a time 

This programme shows how many text files are present in a directory. and -more- command
was included.

For more parameters type  more/?

Sample Programmes :-

@echo off
:: Displays windows  output one screen at a time 

@echo off if not exist *.txt ( echo This directory contains no text files. ) else ( echo This directory contains the following text files: echo. dir /b *.txt > c:\main\test.txt )
type c:\main\test.txt | more
:: pipe sign and more
:: give > or >> 
exit

Open output file name in a directory c:\main\test.txt and check how many text files are there in a directory. 
---------------------------
How check currently running programme?

 Syntax
 
 tasklist


Discription:-

 Display all currently running task including system. (eg task manager)

For more parameters type  tasklist/?

Sample Programmes :-

tasklist   |   more
or 
tasklist

::type this command on dos prompt, check how many programmes are running.
---------------------------

How to check windows version?


 Syntax 
  
 ver   


Discription:-

 Display windows version

For more parameters type  ver/?

Sample Programmes :-

ver

type this command in dosprompt.
---------------------------
How to close programmes?


 Syntax 
  
 taskkill   

Description:-
 
close running application in computer. eg. notepad.exe, paint.exe, wordpad.exe

Examples:-

taskkill.exe /im cmd.exe

taskkill.exe /im notepad.exe

this will close existing Dos prompt window and notepad

Sample programme 1 :-


---------------------------
How to check Remote server is working or not ?

 Syntax 
 ping tracert  

     

Description:-

Check the remote server is working or not:-

PING google.com

PING google.com >> c:\bat\test.txt

tracert:-

In the Command Prompt window, type 'tracert' followed by the destination, either an IP Address or a Domain Name, and press Enter. The command will return output indicating the hops discovered and time (in milliseconds) for each hop

Eg:   tracert google.com
or
tracert 8.8.8.8
---------------------------
Continued to page 03

PAGE 01

PAGE 02

No comments:

Post a Comment

HOW TO BACKUP YOUR ALL DIRECTORY IN YOUR COMPUTER AND FILE NAMES:-

HOW TO BACKUP YOUR ALL DIRECTORY IN YOUR COMPUTER AND FILE NAMES:-   Syntax         tree   /f Description:- Displays names of files in a dir...