Nobody gets any points for astuteness, perspicacity or just plain gumption here! None of you noticed that the OP posted the same number of error messages as there were files. Therefore, the problem was occurring in the loop, not in the FOR line which set it up.
Also, nobody bothered (come on guys!) to type rename /? at the prompt and read the help, whereupon it would have become clear where the problem lies.
Namely in the incorrect syntax being used with the ren / rename command.
The syntax is
RENAME [drive:][path]filename1 filename2
The code was doing this
RENAME [drive:][path]filename1 [drive:][path]filename2
To give a concrete example
This is wrong
Rename "C:\My Documents\My Pictures\My Meals\Cheeseburger105.jpg" "C:\My Documents\My Pictures\My Meals\Hamburger105.jpg"
This is right
Rename "C:\My Documents\My Pictures\My Meals\Cheeseburger105.jpg" "Hamburger105.jpg"
When you have to specify the full path of a file to rename, e.g. because it is not in the current directory, supply it once, before the present (old) filename and then supply just the new name (no path).
It actually says this in the rename /? help
Note that you cannot specify a new drive or path for your destination file.
Dir /s /b always produces a full path for each file, so you need to get at the drive, path, name and extension information using the FOR variable modifiers which you can read about in the FOR /? documentation
Here is some commented code which (I hope) will make all this clear...
@echo off
REM You need this because you are setting variables (and reading them)
REM inside a parenthetical structure (a loop in this case)
setlocal enabledelayedexpansion
REM The /f switch tells FOR to treat the output of the command
REM as a series of text lines to be processed
REM I have changed the variable to upper case to make
REM the variable modification clearer
for /f "delims=" %%A in ('dir /s /b *_MG_*.JPG') do (
REM If %%A is a file path and name (or just a filename)
REM %%~dpA is its drive and path...
set filepath=%%~dpA
REM ... and %%~nxA is its name and extension
REM Thus we have removed the drive and path...
set filenameold=%%~nxA
REM Modify just the name without path
REM to get the new name
set filenamenew=!filenameold:_MG_=!
REM Give the Rename command the full path & old name
REM for the file to be renamed, and just the new name
rename "!filepath!!filenameold!" "!filenamenew!"
)
Since we have now got up to 7 pages, I feel justified in having a moan about how standards are slipping here on CH. Somebody posts a question, somebody else (who shall be nameless) posts a partial solution, the OP tries it out, it doesn't work, and then a whole bunch of people just post guesses. Nobody read the command documentation; nobody tried specimen code.