title: windows batch script for renaming files
body: <p>I have a lot of files in a windows folder. These file names contain a lot of underscores as well. E.g.:</p>
  
  <blockquote>
  <pre><code>ad_iod_errr_opp12.txt  
  ghff_jjk56.txt  
  opr_sdr_ot_wrr_12ee.txt  
  </code></pre>
  </blockquote>
  
  <p>I want to rename these files such that the last underscore and everything following it is removed and the files are renamed as follows:</p>
  
  <blockquote>
  <pre><code>ad_iod_errr.txt
  ghff.txt
  opr_sdr_ot_wrr.txt
  </code></pre>
  </blockquote>
  
  <p>Note: The number of underscores varies. Some filenames may have just a single underscore, whereas other filenames may have up to 8 underscores. I am looking for a  Windows batch script which could do the trick and not PowerShell commands.</p>
  
  <p>This is the code that I am using currently to rename the files considering that the file names have only one underscore in it:</p>
  
  <pre><code>@ECHO OFF 
  SETLOCAL 
  cd C:\Users\ambika.narayan.rao\JenkinsWorkspace 
  SET "fname=*.txt" 
  FOR %%i IN ("%fname%") DO FOR /f "delims=_" %%j IN ("%%i") DO ren "%%~i" "%%~j%%~xi" 
  GOTO :EOF
  </code></pre>