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.
rename file name beginning with a pattern until (unknown) end?
Moderator: DosItHelp
Re: rename file name beginning with a pattern until (unknown) end?
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.
However, this will fail if the file name contains exclamation points.
Steffen
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
Steffen
Re: rename file name beginning with a pattern until (unknown) end?
Or maybe this.
Testing
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.
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%
Code: Select all
C:\Users\Squashman\Desktop>so.bat
NEW=aaa bbb newpattern
Re: rename file name beginning with a pattern until (unknown) end?
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