Page 1 of 1

.bat or .vbs Find within text and Rename File

Posted: 31 Aug 2010 11:35
by jmituzas
I have outputted some text files all in the same directory. Each .txt file has within a group number, this number always starts with RXC and can go upwards of 5 characters afterwards, giving us RXCXXXXX i need the script to find this RXC number and rename the file to its corresponding group number, then do the same for all files in the same directory.

Thanks in advance,
Joe

Re: .bat or .vbs Find within text and Rename File

Posted: 31 Aug 2010 16:27
by orange_batch
You don't need to make a new thread for each step of whatever it is you require, you could just ask more questions in one thread. 8) But at least you detail your questions.

If RXC#~ is always at the beginning of a line,

Code: Select all

setlocal enabledelayedexpansion

set toggle=0
for %%x in ("C:\...\Target Folder\*.txt") do (
  for /f "usebackq delims=" %%y in ("%%x") do (
    if !toggle!==0 (
      set "line=%%y"
      if "!line:~0,3!"=="RXC" ren "%%x" "%%y.txt"&set toggle=1
    )
  )
  set toggle=0
)


Change C:\...\Target Folder.

Edit: Oops, toggles always mess me up. :x Fixed.