title: Read File Name in Windows Batch Programming
body: <p>I want to read the name of a file in windows batch programming. I am trying by using different methods but failed please help.</p>
  
  <p>Scenario has been given below.</p>
  
  <p>I have different files in a folder but the length for filename is same for all files.
  E.g.</p>
  
  <pre><code>1000342578_30062011.PDF
  1000342329_30062011.PDF
  </code></pre>
  
  <p>And I just want to save the part after <code>_</code> and before <code>.PDF</code> (e.g. <code>30062011</code>) part in a variable.</p>
  
  <p>Below are just tries but I am not able to get through.</p>
  
  <pre><code>@echo off
  echo.Date   : %date%
  echo.Year   : %date:~10,4%
  dir /s /b C:\Users\zeeshando\Desktop\*.txt
  for %%x in (c:\temp\*.xls) do echo %%x 
  lfnfor off 
  for %%x in (*.*) do echo %%x &gt; filelist.txt 
  PAUSE
  for %i in (C:\Users\zeeshando\Desktop) do 
      echo %~ni
  PAUSE
  </code></pre>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>My current code, based on @Alex K.'s answer:</p>
  
  <pre><code>@setlocal enabledelayedexpansion
  for %%x in (C:\Hi\*.*) do (
      set fn=%%x
      set fn=!fn:~22,8!
      echo !fn!
      call:handler
  )
  goto:eof
  
  :handler
  echo !fn!
  copy C:\Hi\*.* E:\!fn!
  goto:eof
  
  
  PAUSE
  </code></pre>
  
  <p>But it is not copying the files. Please check the above code.</p>