Below is my dir command:
03/02/2016 02:25 PM <DIR> .
03/02/2016 02:25 PM <DIR> ..
03/01/2016 05:28 PM 342 agent_comp.css
03/01/2016 05:27 PM 1,581 agent_comp.dart
03/01/2016 05:25 PM 3,017 agent_comp.html
03/01/2016 04:54 PM 343 array_comp.css
03/01/2016 05:02 PM 1,965 array_comp.dart
03/01/2016 05:02 PM 2,191 array_comp.html
03/01/2016 04:54 PM 343 server_comp.css
03/01/2016 05:11 PM 1,481 server_comp.dart
03/01/2016 05:16 PM 2,033 server_comp.html
03/02/2016 01:04 PM 1,542 system_status_comp.dart
03/01/2016 04:52 PM 647 system_status_comp.html
11 File(s) 15,485 bytes
2 Dir(s) 223,731,183,616 bytes free
and my goal is to strip _comp
from all the filenames
I was trying ren *_* *.*
but that doesnt work.
My thought process was that in an example:
ren *_* *_NEW.*
replaces all _comp
with _NEW
so i was thinking to just remove that from the second argument as above, but it doesnt work as I wanted.
Where did my logic fault me? My bet is that the regex somehow was lazy and just ended up renaming it to exactly what it was.
Any clue?
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*%~1*" '
) DO (
SET "filename=%%~na"
SET "filename=!filename:%~1=!"
ECHO(REN "%sourcedir%\%%a" "!filename!%%~xa"
)
GOTO :EOF
You would need to change the setting of sourcedir
to suit your circumstances.
The required REN commands are merely ECHO
ed for testing purposes. After you've verified that the commands are correct, change ECHO(REN
to REN
to actually rename the files.
run
*thisbatch* _comp
to delete the string "_comp" from the filenames.
Read each filename matching the mask, substitute nothing for the string specified as %1
BTW, changing to
SET "filename=!filename:%~1=%~2!"
should replace the first supplied string with the second in filenames, so
*thisbatch* _comp
would delete the string "_comp" from the filenames.
and
*thisbatch* _comp abcd
would replace "_comp" with "abcd"