0

I want to replace a variable's substring, previously stored on a variable inside a for loop, I tried to do it like this but it didn't work:

setlocal EnableDelayedExpansion
set checkVar=abcd
FOR %%Y IN (*.pdf) DO (  
    SET meu=%%Y
    CALL SET meuWithoutChar=!meu:%%%checkVar%%%=! 
    ECHO meuWithoutChar=!meuWithoutChar!
)

For example here if %%Y==blepabcdnnnn.pdf; I want to have meuWithoutChar=blepnnnn.pdf on the output Thank you in advance

3
  • 2
    You do not need to use that many percent symbol nor do you need to use the CALL if you are trying to use delayed expansion. The replace variable is outside the loop so you only need a single set of percent symbol. You are attempting to use delayed expansion but you have not enabled it with a SETLOCAL command. Commented Aug 4, 2018 at 17:14
  • 1
    While not required by syntax, proper indenting helps reading the code. Commented Aug 4, 2018 at 17:29
  • I wrote the question a little too fast, sorry I edited it to make it more clear Commented Aug 4, 2018 at 18:08

2 Answers 2

2

You are bit confused on the concept of delayed expansion and the use of CALL to get an extra phase of expansion. Here are the examples. I am just using your single file example. You can change it back to using the wildcard.

CALL example

@echo off
set checkVar=abcd
FOR %%Y IN (blepabcdnnnn.pdf) DO (  
SET "meu=%%Y"
CALL SET "meuWithoutChar=%%meu:%checkVar%=%%" 
CALL ECHO meuWithoutChar=%%meuWithoutChar%%
)
pause

Delayed Expansion

@echo off
setlocal Enabledelayedexpansion
set checkVar=abcd
FOR %%Y IN (blepabcdnnnn.pdf) DO (  
SET "meu=%%Y"
SET "meuWithoutChar=!meu:%checkVar%=!" 
ECHO meuWithoutChar=!meuWithoutChar!
)
pause
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! On my first try I tired CALL SET meuWithoutChar=!meu:%checkVar%=! , but it didn't work. Over the fact that the use of CALL wasn't necessary, I don't know where I messed it up. (sorry for mixing everithing up, I'm not used to code)
1

As a supplement/extension to Squashmans answer.
Only cycles through necessary files and ignores the file's extension.

Without delayed expansion:

@Echo Off
SetLocal DisableDelayedExpansion
Set "strChr=abcd"
For %%A In ("*%strChr%*.pdf") Do (Set "objFileName=%%~nA"
    Call Set "objNewFile=%%objFileName:%strChr%=%%%%~xA"
    Call Echo %%%%objNewFile%%%%=%%objNewFile%%)
Pause

With full script delayed expansion, (will have issues with filenames containing !'s):

@Echo Off
SetLocal EnableDelayedExpansion
Set "strChr=abcd"
For %%A In ("*%strChr%*.pdf") Do (Set "objFileName=%%~nA"
    Set "objNewFile=!objFileName:%strChr%=!%%~xA"
    Echo %%objNewFile%%=!objNewFile!)
Pause

With toggled delayed expansion, (protects filenames containing !'s):

@Echo Off
SetLocal DisableDelayedExpansion
Set "strChr=abcd"
For %%A In ("*%strChr%*.pdf") Do (Set "objFileName=%%~nA"
    SetLocal EnableDelayedExpansion
    Set "objNewFile=!objFileName:%strChr%=!%%~xA"
    Echo %%objNewFile%%=!objNewFile!
    EndLocal)
Pause

2 Comments

Thank you! If I understood, call command can always be used instead of the delayed expansion and reciprocally?
@Blumer, no it cannot. Whilst it may work in the specific case above, the Call command is not an alternative for enabling delayed expansion, (you can't Call If "%%objNewFile%%"=="efgh" ... for instance).

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.