title: cmd and exclamation marks
body: <p>the following code fails for folder names containing exclamation marks. I think I need DelayedExpansion enabled to handle the nested for loops. Any ideas to get this work? Thanks!</p>
  
  <pre><code>@echo off &amp; setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
  
  set "dest=%~dpn1"
  IF "%1" equ "" Set "dest=%cd%"
  
  set /a Folders=0
  set /a FoldersWithSubFoldersAndFiles=0
  set /a FoldersWithOnlyFiles=0
  set /a FoldersWithOnlySubFolders=0
  set /a FoldersEmpty=0
  
  for /f "usebackq tokens=*" %%a in (`DIR /AD /S /B "%dest%"`) do (
      Set /a Folders+=1
      for /f "usebackq tokens=*" %%i in (`DIR "%%a" /A-D /B 2^&gt;NUL^| FIND /C /V ""`) do (
          Set NumberOfFiles=%%i
      )
      for /f "usebackq tokens=*" %%i in (`DIR "%%a" /AD /B 2^&gt;NUL^| FIND /C /V ""`) do (
          set NumberOfFolders=%%i
      )
      IF "!NumberOfFiles!" neq "0" IF "!NumberOfFolders!" neq "0" set /a FoldersWithSubFoldersAndFiles+=1
      IF "!NumberOfFiles!" neq "0" IF "!NumberOfFolders!" equ "0" set /a FoldersWithOnlyFiles+=1
      IF "!NumberOfFiles!" equ "0" IF "!NumberOfFolders!" neq "0" set /a FoldersWithOnlySubFolders+=1
      IF "!NumberOfFiles!" equ "0" IF "!NumberOfFolders!" equ "0" (
          set /a FoldersEmpty+=1
          echo %%a is empty.
      )
  )
  
  echo Found %Folders% folders below "%dest%".
  echo %FoldersWithSubFoldersAndFiles% folders containing files and subfolders.
  echo %FoldersWithOnlyFiles% folders containing files only.
  echo %FoldersWithOnlySubFolders% folders containing subfolders only.
  echo %FoldersEmpty% folders are empty.
  
  endlocal
  </code></pre>