@ECHO OFF
SETLOCAL
SET "string=string1 by string2.txt"
SET "string=%string:* by =%"
ECHO +%string%+
GOTO :EOF
The above SET command will remove the unwanted data. Result shown between + to demonstrate absence of spaces.
Formula: set var=%somevar:*string1=string2%
will assign to var the value of somevar with all characters up to string1 replaced by string2. The enclosing quotes in a set command ensure that any stray trailing spaces on the line are not included in the value assigned.