Today I am going to explain some important stuffs of windows batch,which may help you to do some batch programming
1)-First one is related to admin permission,Generally this is the first step of any batch file program.
To accomplish this task I am showing you to simplest way,
you can check for user net session like
net session >nul 2>&1
if %errorLevel% == 0 (
echo Success: Congrats, You have administrative permissions.
echo.
) else (
echo Failure: Current permissions inadequate. Please contact your NetOps Team
timeout 10 >nul
exit
)
This will check user right
2)-To put blank line between two statement
use echo.
3)-To check user OS version
If you want to get user OS version means user using 64 bit or 32 bit then you can use this statement
IF EXIST "%PROGRAMFILES(X86)%" (set bit=64) ELSE (set bit=32)
if %bit%==64 ( echo user have 64 bit operating system) else ( echo user have 32 bit operating system)
4)-To get first sub-directory name
Assume you have any directory name A and in this directory you have 3 more sub directory name as B,C,D and you want to get name of first listing directory then you can use this stuff,In my program I used this stuff to update java path,
it will search sub directory under "C:\Program Files\Java" and set first directory name in javaPath variable,then you can use this variable where you want
for /f "delims=" %%F in ('dir "C:\Program Files\Java" /b /o-n') do set javaPath=%%F
setx PATH "%PATH%;%javaPath%\bin" /M
5)-To check java is already installed or not
I think for this task registry is best option.
for /f tokens^=2^ delims^=^" %%i in ('reg query HKEY_CLASSES_ROOT\jarfile\shell\open\command /ve 2^>nul') do set javaInstallpath=%%i
Set javaString=Java
Echo.%javaInstallpath% | findstr %javaString%>nul && (
echo It seems java is already installed on your system,
) || (
echo.
echo It seems Java is not installed on your system.
)
2^>nul-Its a optional one ,you can remove this from code,Its used to hide the error message ,if it fails to get java instance on given location
6)-Extraction of zip file using 7-Zip
If you don't want to use vb script you can third party tool for your purpose,
I hope you have 7-Zip in c drive,Then in batch file you can use these three line to extract required zip at required destination,
You can see I have used %~dp0 it have the path of your batch file directory,means if your batch file is in d:\test\batch\testBatch.bat then %~dp0 have path upto d:\test\batch
c:
cd C:\Program Files\7-Zip
7z x "%~dp0\devTool.zip" -o%~dp0\ *.* -r
7)-Downloading software from http location-In my view the best and simple way to accomplish this one is to use power shell
You just need to put 2 line of code in you batch file to download any software or file,it use one ps file you need to provide this file in addition
psFileToDownload.ps1--copy and paste these three line in txt file and save it as
psFileToDownload.ps1
param($url, $filename)
$client = new-object System.Net.WebClient
$client.DownloadFile( $url, $filename)
In Batch
Put these two line in batch file
powershell Set-ExecutionPolicy Unrestricted
powershell -ExecutionPolicy RemoteSigned -File %psPath% %downloadLocation% "%~dp0\TestDownload.zip"
psPath--Path of psFileToDownload.ps1
downloadLocation=The http location from where you want to download any file
TestDownload.zip=The required file will save as TestDownload.zip
8)-To put sleep between to statement you can use timeout
timeout 10>nul
sleep for 10 sec
1)-First one is related to admin permission,Generally this is the first step of any batch file program.
To accomplish this task I am showing you to simplest way,
you can check for user net session like
net session >nul 2>&1
if %errorLevel% == 0 (
echo Success: Congrats, You have administrative permissions.
echo.
) else (
echo Failure: Current permissions inadequate. Please contact your NetOps Team
timeout 10 >nul
exit
)
This will check user right
2)-To put blank line between two statement
use echo.
3)-To check user OS version
If you want to get user OS version means user using 64 bit or 32 bit then you can use this statement
IF EXIST "%PROGRAMFILES(X86)%" (set bit=64) ELSE (set bit=32)
if %bit%==64 ( echo user have 64 bit operating system) else ( echo user have 32 bit operating system)
4)-To get first sub-directory name
Assume you have any directory name A and in this directory you have 3 more sub directory name as B,C,D and you want to get name of first listing directory then you can use this stuff,In my program I used this stuff to update java path,
it will search sub directory under "C:\Program Files\Java" and set first directory name in javaPath variable,then you can use this variable where you want
for /f "delims=" %%F in ('dir "C:\Program Files\Java" /b /o-n') do set javaPath=%%F
setx PATH "%PATH%;%javaPath%\bin" /M
5)-To check java is already installed or not
I think for this task registry is best option.
for /f tokens^=2^ delims^=^" %%i in ('reg query HKEY_CLASSES_ROOT\jarfile\shell\open\command /ve 2^>nul') do set javaInstallpath=%%i
Set javaString=Java
Echo.%javaInstallpath% | findstr %javaString%>nul && (
echo It seems java is already installed on your system,
) || (
echo.
echo It seems Java is not installed on your system.
)
2^>nul-Its a optional one ,you can remove this from code,Its used to hide the error message ,if it fails to get java instance on given location
6)-Extraction of zip file using 7-Zip
If you don't want to use vb script you can third party tool for your purpose,
I hope you have 7-Zip in c drive,Then in batch file you can use these three line to extract required zip at required destination,
You can see I have used %~dp0 it have the path of your batch file directory,means if your batch file is in d:\test\batch\testBatch.bat then %~dp0 have path upto d:\test\batch
c:
cd C:\Program Files\7-Zip
7z x "%~dp0\devTool.zip" -o%~dp0\ *.* -r
7)-Downloading software from http location-In my view the best and simple way to accomplish this one is to use power shell
You just need to put 2 line of code in you batch file to download any software or file,it use one ps file you need to provide this file in addition
psFileToDownload.ps1--copy and paste these three line in txt file and save it as
psFileToDownload.ps1
param($url, $filename)
$client = new-object System.Net.WebClient
$client.DownloadFile( $url, $filename)
In Batch
Put these two line in batch file
powershell Set-ExecutionPolicy Unrestricted
powershell -ExecutionPolicy RemoteSigned -File %psPath% %downloadLocation% "%~dp0\TestDownload.zip"
psPath--Path of psFileToDownload.ps1
downloadLocation=The http location from where you want to download any file
TestDownload.zip=The required file will save as TestDownload.zip
8)-To put sleep between to statement you can use timeout
timeout 10>nul
sleep for 10 sec
Thanks for such a nice explanation,It's help me alot to automate my some task
ReplyDelete