0

I have the following string:

9/14/22 11:00:12,,,0,0,,,,,,

I need to remove 9/14/22 11:00:12 so I only have ,,,0,0,,,,,, left over.

@echo off
set str=9/14/22 11:00:12,,,0,0,,,,,,
FOR /F "tokens=1* delims=," %%a in ("%str%") DO (
    set REMOVE_STR=%%a
)
set REMAINING_STR=%str:;%REMOVE_STR%;=%
echo New string: %REMAINING_STR%

The output is just the original string, so I know there's a problem with the substr removal step. But, I can't figure out what I'm doing wrong.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
cmarti10
  • 51
  • 4
  • Well, why do you include semicolons in your replacement string, even though no such are present in your original string? Anyway, the expression `%str:;%REMOVE_STR%;=%` becomes interpreted as an undefined variable `%str:`, a literal `;`, the variable `%REMOVE_STR%`, the literal string `;=` and an orphaned `%`; using `call` would solve that, like `call REMAINING_STR=%%str:;REMOVE_STR%;=%%`, or actually better `call "REMAINING_STR=%%str:*REMOVE_STR%=%%"`… – aschipfl Sep 18 '22 at 12:14

2 Answers2

1

You need delayed expansion to use variables inside a code block that you defined/changed within the same block:

@echo off
setlocal enabledelayedexpansion 

set str=9/14/22 11:00:12,,,0,0,,,,,,
FOR /F "tokens=1 delims=," %%a in ("%str%") DO ( 
  set "remaining_str=!str:%%a=!"
  echo New string: !Remaining_Str!
)

Output:

New string: ,,,0,0,,,,,,
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Ok, I see. So, I tried using %%b, but I'm getting some behaviors I don't fully understand. When I echo REMAINING_STR outside the FOR loop, I get "0,0,,,,,," which is close, but I also need the leading commas in the string. If I echo REMAINING_STR inside the FOR loop, I get the original string str. I'm not sure why? – cmarti10 Sep 17 '22 at 15:10
  • Ah, sorry - I missed the `,,,` thing. Consecutive delimeters are treated as one. Changed my answer to fit your needs (you were right, with your desired output, you need the "remove first part" thingy) – Stephan Sep 17 '22 at 15:46
1

You don't need a for loop, or delayed expansion for that task, you can do it with direct variable expansion and sustitution, just like this:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

Set "str=9/14/22 11:00:12,,,0,0,,,,,,"

Set "REMAINING_STR=,%str:*,=%
Echo New string: %REMAINING_STR%

The above substitutes everything up to, and including, the first comma, with nothing, in the expanded variable, and precedes that with the initially first removed comma.

You could alternatively substitute everything up to, and including, the first comma, with a comma, like this:

Set "REMAINING_STR=%str:*,=,%
Compo
  • 36,585
  • 5
  • 27
  • 39