of course:
RENAME "%%I" "%%~n
I!Log.rtf"
But it give output data1
log.rtf and data2
log.rtf
btw. what do you try to achive with setlocal delayedexpansion
and that single !
?
EDIT: if you insist in doing it with for
(because perhaps, you have many files to rename):
@echo off
setlocal enabledelayedexpansion
for %%i in (*.txt) do (
set file=%%i
set file=!file:Data=Log!
set file=!file:.txt=.rtf!
echo ren %%i !file!
)
the first set
sets the variable file
to the filename
the second one replaces Data
with Log
the third one replaces .txt
with .rtf
then rename original filename (%%i
) to the changed filename (!file!
)
(the echo
is there to just show the command on screen instead of really renaming files. Good for testing. If you are sure, that the code does, what you want, just remove the echo
)
the setlocal enabledelayedexpansion
is needed to use a variable, that is changed inside a block (between (
and )
) inside the same block. Therefore you have to use the variable as !var!
instead of %var%
Note: of course this code could be improved, but as a beginner: keep it as simple (and readable) as possible. For example, this code will replace Data
, but not data
or DATA
, it will not handle spaces in filenames, ...