-3

I am trying to rename a file with two variables in a batch file. I have file with name foo_hello.txt and I want to rename it to foo_20_hello_50.txt.

20 and 50 are two different #define in the same header file.

I was successful in renaming it with one variable.

for /f "tokens=3" %%i in ('FINDSTR /C:"Place" ..\header.h') do ren foo_hello.txt foo_hello_%%i.txt

but I am not sure how to add the other variable in the filename.

Sid Saxena
  • 23
  • 7

1 Answers1

0

Your question have several missing details: What is the format of the input file? Is there just one line with each value? What value is placed first in the file? or, they may come in any order...

The Batch file below assume that the values are placed in "Place" then "Watch" order. If this is not true, the code needs to be changed...

@echo off
setlocal

set "Place="
for /f "tokens=3" %%i in ('FINDSTR "Place Watch" header.h') do (
   if not defined Place (
      set "Place=%%i"
   ) else (
      set "Watch=%%i"
   )
)

ECHO ren foo_hello.txt foo_%Watch%_hello_%Place%.txt

Input file:

First line
#define Place 50
Mid line
#define Watch 20
Last line

Output example:

ren foo_hello.txt foo_20_hello_50.txt
Aacini
  • 65,180
  • 12
  • 72
  • 108