I am trying to create a batch file to automatically rename the files contained in a folder from this structure: A12345678.textornumbers.textornumbers.txt to a simpler 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 errors, but does nothing ...
Edition:
Well, the problem can be seen in a different way: I want to rename the file, leaving the first 9 characters and the extension.
I saw a similar question: Removing characters from a file name
I modified and adjusted 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! File Name! .Txt
This is for the first image in directoy, and errors (such a file already exists) for others.
EDITED 2:
Thanks to the answers and other information that I found on the Internet, here is the solution I had: to delete the last characters of the file name, because I want to stay in the first nine 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 ( from Carlitos.dll ). Hope this helps other people with similar problems. Thanks for your ideas and help!