title: Delayed expansion and exclamation marks in strings
body: <p>Ok, so I'm still pretty new to batch scripting and I have this problem with my code that I'm using setlocal enabledelayedexpansion in my code for the for loop, the For loop goes through folder names in a specific directory, but the problem is that some of the names may include "!"(exclamation marks) and if that is the case they are not counted for in the "!filename!" and when the code creates a new directory it does not include the "!". Also when the xcopy tries to copy the files from the original folder, the folder is not found, because the variable "!filename!" is not the same as the original(does not have the exclamation point).</p>
  
  <p>So I found that for this I need to only add "setlocal enable delayed expansion" to some parts of the code and turn it off at other, but I cant seem to find the right places.</p>
  
  <p>The code:</p>
  
  <pre><code>@ECHO OFF
  setlocal enabledelayedexpansion
  
  SET Location_Folder=V:
  SET Destination_folder=V:\Nonimportable
  
  SET Check_file_validation=V:\Nonimportable\result.txt
  
  SET Excluded_folder=Nonimportable
  set "lineNr=12"
  
  For /f "tokens=*" %%O in ('dir /b /a:d "%Location_Folder%"') do (
      set filename=%%O
  
      call D:\somefolder\otherfolder\batscriptname.bat !filename!
      set Validation=
  
      echo !filename!| FINDSTR /i /c:"%Excluded_folder%" &gt;NUL
      IF ERRORLEVEL 1 (
  
          for /F "skip=12 delims=" %%a in (%Check_file_validation%) do if not defined Validation (
                  set Validation=%%a
                  call :valid
          )
      ) else (
          echo This folder name is excluded: !filename!
      )
  )
  goto Finish
  :valid 
  
  echo !Validation!| FINDSTR /c:"1" &gt;NUL
      if ERRORLEVEL 1 (
  
          set Folder_path=%Location_Folder%\!filename!
          set New_Folder_path=%Destination_folder%\!filename!
  
          mkdir "!New_Folder_path!"
  
          echo D | xcopy /o /y /q /s /v "!Folder_path!" "!New_Folder_path!"
  
          rmdir /s /q "!Folder_path!"
      ) else (
  
          echo Folder is valid !filename!
          goto Finish
      )
  :Finish
  exit /b 
  </code></pre>
  
  <p>The Call part calls another small (~5lines) batch file that checks the sqlplus server if the "!filename!" is valid</p>
  
  <p>EDIT: The whole code works fine and does what it should, unless there is a "!" in the name of some folder.</p>