I try to create a batch file to automaticaly rename the files contained in a folder, from this structure: A12345678.textornumbers.textornumbers.txt to a more simple one: A12345678.txt
I tried something like this:
@echo off
ECHO Renaming files
Pause
:begin
ECHO Renaming txt files
for /r %%x in (A*.*.*.txt) do (REN "%%x" A*.txt)
ECHO Renaming finished
:end
pause
done
It does not return any error, but it does nothing…
EDITED:
Ok, the problem could be seen in other way: i want to rename the file remaining the first 9 characters and the extension.
I saw a similar question here:
Deleting characters from filename
I modified and adjusted to my case to have this:
@echo off
ECHO Renaming files
Pause
:begin
REM setlocal enabledelayedexpansion (the result is the same with and without this line)
set X=9
ECHO Renaming files
for /r %%f in (*.txt) do if %%f neq %~nx0 (
set "filename=%%~nf"
set "filename=!filename:~%X%,-%X%!"
ren "%%f" "!filename!%%~xf")
ECHO Done
ECHO Processing finished
:end
pause
done
But the result is this:
Blockquote
!filename!.txt
This is for the first image in the directoy, and errors (Such file already exist) for the others.
EDITED 2:
Thanks to the replies and other information i found on internet, here is the solution i had: to remove the last characters of the filename, because i want to remain ever the first 9 characters:
@echo off
ECHO renaming files
ECHO.
Pause
:begin
set ext=QUB
set num=17
FOR /f "tokens=*" %%f in ('dir /b /a *.%ext%') do call :lab %%f
ECHO Done
pause
Exit
:lab
set original=
set original=%*
set newname=
call set newname=%%original:.%ext%=%%
call set newname=%%newname:~0,-%num%%%.%ext%
if "%newname%"==".%ext%" (goto :eof)
ren "%original%" "%newname%"
ECHO %newname%
goto :eof
This is not my code, but the solution i used from others (by Carlitos.dll). I hope it could help to others with similar problems. Thanks for your ideas and help!
The script does do something, but nothing useful – It renames each file to it’s original name 🙁
It all has to do with the rules for how REN works with wildcards. I had never seen any proper explanation of how REN works posted anywhere. So a few weeks ago I did extensive experiments and developed a set of rules that explain all the behavior I observed.
You can find my results at How does the Windows RENAME command interpret wildcards? on the StackExchange SuperUser site.
Your problem is easily solved by replacing
*with many?in your target name. Just make sure the number of?is greater than or equal to the max leading name length that you will process. You also don’t need to specify the leadingAif you are not changing the value.The above must iterate each file. It may be a bit faster to iterate the folders only, though I haven’t tested
It is also possible to parse the name with FOR /F so that you don’t have to worry about the number of
?.EDIT based on revised question
To simply preserve up to the 1st 9 characters and the
.txtextension, the solution is even easier, just use 9?:If you want to rename all files, not just
.txtfiles, then