Monday, 12 August 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.