0

Again, I'm struggling with Batch files on windows, and again I hope you can help me again. I don't understand a thing of it ;)

I now have the following code that prints the name of the underlying directories when the directory contains 'R1' in its name.:

FOR /d %%d in (directory\*R1*) do (
SET x=%%d && echo %x%)

I now have the directory name as a string, set in 'x'. I want to split this string on the \ and get the last item. Then I want to replace 'R1' with 'R2' in that last item. Can anyone make a function for me that works? I'm lost.

Thanks.

Coryza
  • 231
  • 1
  • 3
  • 12

1 Answers1

0
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /d /R %%d in (.\*R1*) do (
 SET "x=%%~nxd"
 SET "x=!x:R1=R2!"
 IF /i not "!x!"=="%%~nxd" ECHO old : "%%d" new : "%%~dpd!x!"
)

GOTO :EOF

This should do what you want. The /R switch on the FOR means "and subdirectories, too", and I changed your directory to . for my testing.

Since you don't appear to say what to want to do with the strings, I've simply echoed to old and new versions.

The if statement will filter out only those directories that have R1 somewhere in the last "leaf" of the directory tree.

Magoo
  • 77,302
  • 8
  • 62
  • 84