0

Why is it, that every time I try to use a variable with a ":" it just doesn't use it as all documentation tells me it would. I am quite frustrated at this point. Please help me out guys!

What I want to do is to manipulate a path in different ways (replacing, extracting substring):

  • 1: set /p B=%B :OLD=NEW %
  • 2: set /p B=%~dp0 :OLD=NEW
  • 3: set /p B=%~dp0 :OLD=NEW%
  • 4: filepath[!geil!]=%filepath[!geil!] :%parent%= %
  • 5: filepath[!geil!]=%filepath[!geil!] :!parent!= %
  • 6: set filepath[!geil!]=%%~dpi :22
  • 7: set filepath[!geil!]=%%~dpi :22%
  • I also tried further variations, that didn't work either

These Parts don't work. Are there always syntax mistakes? I always end up with literal outputs. Running on Windows 10 Pro

1.
set /p B=%~dp0 
rem (C:\Folder1\Folder2\FolderOLD\)
set /p B=%B:OLD=NEW%
rem (C:\Folder1\Folder2\FolderNEW\)
pause

2.
set /p B=%~dp0:OLD=NEW
echo %B%

3.
set /p B=%~dp0:OLD=NEW%
echo %B%

4.
setlocal EnableDelayedExpansion
set parent=%~dp0
set /a geil=0
pause
for /r %%i in (*) do (set filepath[!geil!]=%%~dpi & set filepath[!geil!]=%filepath[!geil!]:%parent%=% & set /a geil+=1)

5.
setlocal EnableDelayedExpansion
set parent=%~dp0
set /a geil=0
pause
for /r %%i in (*) do (set filepath[!geil!]=%%~dpi & set filepath[!geil!]=%filepath[!geil!]:!parent!=% & set /a geil+=1)

6.
setlocal EnableDelayedExpansion
set parent=%~dp0
set /a geil=0
pause
for /r %%i in (*) do (set filepath[!geil!]=%%~dpi:22 & set /a geil+=1)

7.
setlocal EnableDelayedExpansion
set parent=%~dp0
set /a geil=0
pause
for /r %%i in (*) do (set filepath[!geil!]=%%~dpi:22% & set /a geil+=1)
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 2
    The first thing you need to do is remove the `/P` option from your `Set` commands. The `/P` option is for requesting user input, which I'm assuming is not intended. To ensure that you understand how to use the `Set` command, open a Command Prompt window, type `set /?`, press the `[ENTER]` kety, and read its usage information. Unfortunately as you've now noticed, because the colon is part of the expansion and substitution syntax, you cannot include it in the string using the standard syntax, and will need to find an alternative replacement methodology. – Compo Nov 29 '20 at 15:10
  • Ah thanks! This sorts the 1. out. How can I make 2. and 3. work as there the "/p" doesn't seem to be the only problem? – old_edge Nov 29 '20 at 15:27
  • 1
    You should step back a bit and learn one thing after another. At first, sub-string substitution only works on normal environment variables but not on argument references like `%0`, `%1`, etc. Then you are trying to use some kind of nested variables, or (pseudo-)arrays, or whatever one may call them, which you seem not to really understand (refer to [this](https://stackoverflow.com/a/10167990)). So let me recommend to learn each or these things before trying to combine them… – aschipfl Nov 29 '20 at 16:04
  • So you are saying that 2. and 3. are not possible and the workaround of 1. is the only way to achieve this? Thank you for the link. Would you please point out which part doesnt make sense to you and why? Thanks so much for your help. – old_edge Nov 29 '20 at 16:34

0 Answers0