Reputation: 1116
I have a file named 01_tiago_miguel.txt
and this code:
setlocal EnableExtensions DisableDelayedExpansion
for %%A in ("*_*") do (
rem // Store current item:
set "FILE=%%~A"
rem // Toggle delayed expansion to avoid loss of or trouble with `!`:
setlocal EnableDelayedExpansion
rem // Remove everything up to and including the first `_`:
echo ren "!FILE!" "!FILE:*_=!"
)
endlocal
exit /B
This gives the output:
ren "01_tiago_miguel.txt" "tiago_miguel.txt"
Which is exactly what I want - remove the first three characters.
However if I remove the echo
it renames to miguel.txt
, which doesn't make much sense since the output of the echo seems correct.
Any idea why?
Upvotes: 0
Views: 489
Reputation: 79982
Use
for /f "delims=" %%A in (`dir /b /a-d "*_*"') do (
What is happening is that your echo
doesn't rename the file (duh) but removing it does.
ren "01_tiago_miguel.txt" "tiago_miguel.txt"
is executed, so the new filename is tiago_miguel.txt
,
BUT
tiago_miguel.txt
also fits the *_*
mask, so
ren "tiago_miguel.txt" "miguel.txt"
is executed, so the new filename is miguel.txt
.
Using the dir
command does a directory list and then processes the list, so any changes made are not re-encountered.
Upvotes: 1