.bat or .vbs Find within text and Rename File

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jmituzas
Posts: 13
Joined: 30 Aug 2010 08:43

.bat or .vbs Find within text and Rename File

#1 Post by jmituzas » 31 Aug 2010 11:35

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

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

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

#2 Post by orange_batch » 31 Aug 2010 16:27

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.

Post Reply