0

I have multiple files named ".txt.jpg" (don't ask...) in a single directory. I would like to remove the ".txt" portion so the result becomes ".jpg" only so I can then import them into Photos.

Any suggestions? I looked at previous Q/A involving the mv command but couldn't quite figure out how to write the correct command. I don't want to mess further. Thanks

lake monster
  • 19
  • 1
  • 2
  • 1
    What is your Operating System? Scripting language? As it stand your question is impossible to answer. Also, please note that [SU] is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read [How do I ask a good question?](https://superuser.com/help/how-to-ask). – DavidPostill Sep 04 '16 at 07:27
  • 1
    @DavidPostill At first look, I thought he meant batch as in "batch programming in Windows" as the tags of the question were *command-line* and *batch*, but on further reading, he has mentioned the mv command, which means he is talking about Unix/Linux. – Don't Root here plz... Sep 04 '16 at 13:34
  • Have you tried just using the files as-is. Some software doesn't care about multiple extensions and uses the last one. – fixer1234 Sep 05 '16 at 04:17

1 Answers1

3

If you are on Windows, and if there are no additional dots in any of the file names, then you can use the following:

ren *.txt.jpg ???????????????????????????????????????????????????.jpg

There must be enough ? to match the length of the longest file name. See How does the Windows RENAME command interpret wildcards? for more info.

If some files have more than two dots, then you will need more than a simple REN command. The following should remove the unwanted .txt regardless how many dots are in the original name.

for %A in (*.txt.jpg) do @for %B in ("%~nA") do @ren "%A" "%~nB.jpg"

If you put the command within a batch script then you must double the percents:

@echo off
for %%A in (*.txt.jpg) do for %%B in ("%%~nA") do ren "%%A" "%%~nB.jpg"
dbenham
  • 11,194
  • 6
  • 30
  • 47
  • Could you show how to remove __repeated__ (arbitrary) extensions? That is, `aaa.txt.txt` and `bbb.txt.txt.txt` should become `aaa.txt` and `bbb.txt`, but `ccc.jpg.png` should not be changed. I would be happy to create a separate question if you think it would be better. – john c. j. Apr 16 '20 at 10:44
  • @johnc.j. - That is not easy to do with pure batch. But I have written a [JREN.BAT regular expression renaming utility](https://www.dostips.com/forum/viewtopic.php?f=3&t=6081) (hybrid JScript/batch) that simplifies the task: `jren "(\.[^.]+)\1+$" "$1"` – dbenham Apr 16 '20 at 13:27
  • Very interesting, I will take a look today :) – john c. j. Apr 16 '20 at 14:05