Showing posts with label Batch File. Show all posts
Showing posts with label Batch File. Show all posts

Sunday, August 11, 2013

Batch File - Directory operations


In this article we are going to see how to delete a folder and subfolders using batch files,

          FOR /D %%i IN ("E:\delfolder\*") DO RD /S /Q "%%i" DEL /Q "E:\delfolder\*.*"
If you want to leave a specific folder from delete, Then Make that folder as Hidden first after the deletion make it as visible.

         ATTRIB +H D:\delfolder\hiddenfolder
         FOR /D %%i IN ("E:\delfolder\*") DO RD /S /Q "%%i" DEL /Q "E:\delfolder\*.*"

         ATTRIB -H D:\delfolder\hiddenfolder


we are going to see how to make a progress indication in batch file execution .

       @ECHO OFF
       set "max=11"
       call :init %max% "Window Title: [PPP]"
       for /l %%N in (1,1,%max%) do (
              ping -n 2 -w 1 127.0.0.1>NUL
              call:doProgress
        )
      GOTO:EOF


      :init max format
      set /a "ProgressCnt=-1"
      set /a "ProgressMax=%~1"
      set "ProgressFormat=%~2"
      if not defined ProgressFormat set "ProgressFormat=[PPPP]"
      set "ProgressFormat=%ProgressFormat:[PPPP]=[P] completed.%"
      call:doProgress
      EXIT /b


     :doProgress
     set /a "ProgressCnt+=1"
     SETLOCAL ENABLEDELAYEDEXPANSION
     set /a "per100=100*ProgressCnt/ProgressMax"
     set /a "per10=per100/10"
     set /a "per10m=10-per100/10-1"
     set "P=%per100%%%"
     set "PP="
     for /l %%N in (0,1,%per10%) do call set "PP=%%PP%%*"
     for /l %%N in (%per10%,1,9) do call set "PP=%%PP%% "
     set "PPP="
     for /l %%N in (0,1,%per10m%) do call set "PPP=%%PPP%%*"
     set "ProgressFormat=%ProgressFormat:[P]=!P!%"
     set "ProgressFormat=%ProgressFormat:[PP]=!PP!%"
     set "ProgressFormat=%ProgressFormat:[PPP]=!PPP!%"
     title %ProgressFormat%
     EXIT /b


From the above two code snippets, you can learn some basic concepts of batch files.


 

Saturday, August 10, 2013

Batch File Creation - Basics

       To create a batch file, open a notepad and save the file with an extension of  ".bat". Type the cmd in run and enter , execute the batch file by drag and drop the .bat file into the command prompt and pass the parameters.

Let we start with some basics .

     keyword REM indicates that this line is a comment by using this we can specify the comment for what usage the code is written.
                      REM test 

keyword @echo off indicates that this will stop the several lines of repetitive output from showing
                      @echo off

keyword %1, %2 indicates there are two input parameters for the batch file
                      START %1



Example 1
  Launch a website in default browser

  REM Launch a Website in default browser by getting the input from user
    @ECHO OFF
    START %1
    @ECHO OFF

  Type following line in command prompt, Now you can see the website is launched in default browser.
         C:\test.bat www.dotnetvisio.blogspot.com



Example 2
  Launch a website in specified browser

  REM Launch a Website in specified browser by getting the input from user
    @ECHO OFF
    START /D "C:\PROGRAM FILES (X86)\INTERNET EXPLORER" IEXPLORE.EXE %1
    @ECHO OFF

  Type following line in command prompt,now you can see the website is launched in specified browser.
         C:\test.bat www.dotnetvisio.blogspot.com 



Example 3
  Copy a File from a path to desktop

  REM Copy a file from specified path to desktop
    @ECHO OFF
    COPY %1%USERPROFILE%\DESKTOP
    @ECHO OFF

  Type following line in command prompt,to copy a specified to desktop
         C:\test.bat "c:\c#.pdf"



Example 4
  Iterate to the parent directory and moved to the adjacent directory and change the parent directory.

:: Changes from the current directory to an adjacent directory or to the parent directory
@ECHOOFF
ECHOBatch started

IF"%1" =="1" GOTO Parent-1
IF"%1" =="2" GOTO Parent-2
IF"%1" =="3" GOTO Parent-3

:Parent-1
CD..\%2
GOTOEND

:Parent-2
CD..\..\%2
GOTOEND

:parent-3
CD..\..\..\%2
GOTOEND

:END
      CD %dir%

Input :
                                                                                                                           
         C:\Users\s\Desktop\test.bat 1 documents                                                          


output:

  Batch Started                                                                                                            
      C:\Users\s\Documents