Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8639393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:59:28+00:00 2026-06-12T10:59:28+00:00

I try to create a batch file to automaticaly rename the files contained in

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:59:29+00:00Added an answer on June 12, 2026 at 10:59 am

    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 leading A if you are not changing the value.

    for /r %%x in (A*.*.*.txt) do ren "%%x" ?????????????????????.txt
    

    The above must iterate each file. It may be a bit faster to iterate the folders only, though I haven’t tested

    for /r /d %%x in (.) do ren "%%x\A*.*.*.txt" ?????????????????????.txt
    

    It is also possible to parse the name with FOR /F so that you don’t have to worry about the number of ?.

    for /r %%x in (a*.*.*.txt) do (
      for /f "delims=." %%n in ("%%~nx") do ren "%%x" "%%n.txt"
    )
    

    EDIT based on revised question

    To simply preserve up to the 1st 9 characters and the .txt extension, the solution is even easier, just use 9 ?:

    for /r /d %%x in (.) do ren "%%x\*.txt" ?????????.txt
    

    If you want to rename all files, not just .txt files, then

    for /r %%F in (*) do ren "%%F" "?????????%%~xF"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im try to create a spinner dynamically on press of a button. this is
I try to create a new file inside a JSP and try to save
i have a batch(backup.bat)file.in that i have written a command to create a backup
Using Windows XP, I created a batch-file that takes two files as arguments, and
In Windows cmd batch, I try to call a batch file: call .\%%R\scenery\list bgl.bat
Trying to create a batch to convert .WTV files to mpg files. The code
I'm trying to create a batch file that will check the existence of a
I tried to create a batch file that can backup all my databases. My
I try to create a cronjob on windows7 iis. I downloaded the wget.exe from
I have a VBScript file which I am trying to call from a Batch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.