rename file name beginning with a pattern until (unknown) end?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pstein
Posts: 125
Joined: 09 Nov 2011 01:42

rename file name beginning with a pattern until (unknown) end?

#1 Post by pstein » 11 Aug 2021 15:33

Ok, I can rename a filename and replace a certain pattern by another pattern with a (simplified) command like:

set "search=foobar"
set "replace=newpattern"
set "fname=aaa bbb foobar ccc ddd"

ren "!fname!" "!fname:%search%="%replace%!"

which yields as new filename:

aaa bbb newpattern ccc ddd

It works.
Ok, now the advanced task is to replace the partial filename string BEGINNING with foobar until the end by the new replace string.
Possible file name extensions should not be changed

The final filename should look like

aaa bbb newpattern

How can I achieve this?

It is granted that the file with mentioned file name exists.


aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: rename file name beginning with a pattern until (unknown) end?

#3 Post by aGerman » 11 Aug 2021 16:01

Another possibility is to replace the search pattern with a single character that can't be part of a file name (e.g. a question mark). This can be used as delimiter in a FOR /f loop to tokenize the name.

Code: Select all

@echo off &setlocal EnableDelayedExpansion
set "search=foobar"
set "replace=newpattern"
set "fname=aaa bbb foobar ccc ddd.ext"

for /f "tokens=1* delims=?" %%i in ("!fname:%search%=?!") do ECHO ren "!fname!" "%%i%replace%%%~xj"
PAUSE
However, this will fail if the file name contains exclamation points.

Steffen

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: rename file name beginning with a pattern until (unknown) end?

#4 Post by Squashman » 11 Aug 2021 16:16

Or maybe this.

Code: Select all

@echo off
set "search=foobar"
set "replace=newpattern"
set "fname=aaa bbb foobar ccc ddd"

SETLOCAL ENABLEDELAYEDEXPANSION
SET "END=!fname:*%search%=!"
ENDLOCAL & SET "END=%END%"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "NEW=!fname:%search%%end%=%replace%!"
ENDLOCAL & SET "NEW=%NEW%"
echo NEW=%NEW%
Testing

Code: Select all

C:\Users\Squashman\Desktop>so.bat
NEW=aaa bbb newpattern
There is other logic you could use where you find the position of your search parameter as well and then use that as a substring.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: rename file name beginning with a pattern until (unknown) end?

#5 Post by Squashman » 11 Aug 2021 16:41

You can also find the position of the search string in the content and then substring it.

Code: Select all

@echo off
set "search=foobar"
set "replace=newpattern"
set "fname=aaa bbb foobar ccc ddd"

SETLOCAL ENABLEDELAYEDEXPANSION
call :InStr "%fname%" "%search%" Loc
SET "NEW2=!fname:~0,%Loc%!%replace%"
ENDLOCAL & SET "NEW2=%NEW2%"
echo NEW2=%NEW2%

GOTO :EOF

REM Subroutine to locate a string within another string and return the 0 based offset (-1 indicates not found)
:Instr [target-string] [search-string] [loc-var]
  set "%~3=-1" & set "_str1=%~1"
  call :StrLen "%~1" _len1 & call :StrLen "%~2" _len2 & set /A "_chk=_len1-_len2"
  for %%A in (!_len2!) do (for /L %%B in (0,1,!_chk!) do (if /I "%~2" == "!_str1:~%%B,%%A!" (set "%~3=%%B" & exit /b)))
  exit /b

REM Subroutine to return the length of a string variable
:StrLen [string] [len-var]
  set "_str=A%~1" & set "_len=0"
  for /L %%A in (12,-1,0) do (set /a "_len|=1<<%%A" & for %%B in (!_len!) do if "!_str:~%%B,1!"=="" set /a "_len&=~1<<%%A")
  set /a %~2=%_len%
  exit /b

Post Reply