K
K
kristian_bells2019-11-24 17:05:40
cmd/bat
kristian_bells, 2019-11-24 17:05:40

How to write a bat file that only renames part of the filename?

Hello! I ask you to help with one problem that I can not solve due to my poor skills in working with bat-files.
There are several files:
setup.exe
setup-1.bin
setup-2.bin
We need to write a script that will transfer these files to another folder and give them the following names:
setup_ver2.exe
setup_ver2-1.bin
setup_ver2-2.bin
That is, we after "setup" we add "_ver2", the file extension remains the same.
There is such a script:
@echo off
for %%a in (c:\temp\setup*.*) do (
move "%%~a" "<path to the desired folder>\%%~na_ver2%%~xa"
)
but it generates the following set of files: setup_ver2.exe, setup-1_ver2.bin, setup-2_ver2.bin, which doesn't suit me.
Thank you in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2019-11-24
@res2001

@echo off
SetLocal EnableDelayedExpansion

for %%a in (c:\temp\setup*.*) do (
  set "namesuffix=%%~na"
  set "namesuffix=!namesuffix:setup=!"
  move "%%~a" "<путь к нужной папке>\setup_ver2!namesuffix!%%~xa"
)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question