A12345678.textornumbers.textornumbers.txt - A12345678.txt

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!

+4
source share
3 answers

The script does something but nothing useful - it renames each file to its original name: - (

All this is due to the rules for using REN with wildcards. I have never seen the right explanation of how REN works anywhere. So, a few weeks ago, I conducted extensive experiments and developed a series of rules that explain all the behavior that I observed.

You can find my results in How does the Windows RENAME team interpret wildcards? on the StackExchange SuperUser website.

? your problem easily resolved by replacing * with many ? in your target name. Just make sure the number ? greater than or equal to the maximum leading name length that you will process. You also do not need to specify a leading A unless you change the value.

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

The above should iterate through each file. This might be a little faster for iterating only folders, although I have not tested

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

It is also possible to parse the name using FOR / F, so you don't have to worry about quantity ? .

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

EDIT based on revised question

To just save up to 1 9 characters and the .txt extension, the solution is even simpler, 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" 
+4
source

You can also do something like this:

 for %%x in (*.textornumbers.txt) do ( set nam=%%x if "!nam!" neq "!nam:.textornumbers=!" ren %%x !nam:.textornumbers=! ) 
0
source

Using ~ -modifiers, you can do the following:

  • Take only the name from the original full path and name (& extension):

     FOR /R %%A IN (A*.*.*.txt) DO ( ... %%~nA ... ) 

    This will turn D:\path\A*.*.*.txt only into A*.*.* .

  • Take only the name from result # 1:

     FOR /R %%A IN (A*.*.*.txt) DO ( FOR %%B IN ("%%~nA") DO ( ... %%~nB ... ) ) 

    This will leave you with A*.* .

  • Take only the name from # 2:

     FOR /R %%A IN (A*.*.*.txt) DO ( FOR %%B IN ("%%~nA") DO ( FOR %%C IN ("%%~nB") DO ( ... %%~nC ... ) ) ) 

    which will give you A* .

  • Extract the extension from the original name and combine it with C # 3. This will be the final name A*.txt , which you can now pass to the RENAME command:

     FOR /R %%A IN (A*.*.*.txt) DO ( FOR %%B IN ("%%~nA") DO ( FOR %%C IN ("%%~nB") DO ( RENAME "%%A" "%%~nC%%~xA" ) ) ) 
0
source

Source: https://habr.com/ru/post/1437979/


All Articles