Gregor Isack
Gregor Isack

Reputation: 1131

Extract and replace string from filename in Batch Script

I try to extract the filename from a list of files, replace specific string and feed that into a process. However I got stuck at replace. Any idea?

@echo on
setlocal ENABLEDELAYEDEXPANSION

for %%f in (*.txt) do (

            REM echo %%~nf
            set filename=%%~nf
            echo %@filename%
            set Replaced=replaced
            set @ver=!%filename:ToBeReplace=%Replaced%!
            echo %@ver%    
            )

The echo from ver is not what I expected. Thanks in advance!

Upvotes: 0

Views: 2782

Answers (1)

npocmaka
npocmaka

Reputation: 57252

I have a file called something.txt in the same folder as the following script:

@echo off
setlocal ENABLEDELAYEDEXPANSION

set "old=some"
set "new=else"
for %%f in (*.txt) do (

            REM echo %%~nf
            set "filename=%%~nf"
            echo !filename!
            rem set Replaced=replaced
            set "@ver=!filename:%old%=%new%!"
            echo !@ver!    
)

the optuput after execution is:

something

elsething

Upvotes: 1

Related Questions