0

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!

6
  • you need a delayed expansion Commented Jan 11, 2018 at 13:00
  • I did though. The code is included Commented Jan 11, 2018 at 13:01
  • when you are using delyaed expansion you need to use ! instead of % you can combine it with % when you are replaceing a string - set @ver=!filename:ToBeReplace=%Replaced%! if %replaced% is defined before the brackets block Commented Jan 11, 2018 at 13:04
  • It still echoed empty, I still can't figure out what's wrong :/ Commented Jan 11, 2018 at 13:16
  • What did you expect? What did you get instead? Commented Jan 11, 2018 at 13:20

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I realized I should use !.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.