Shorten file names with REN

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
findstr
Posts: 17
Joined: 09 Jun 2021 12:36

Shorten file names with REN

#1 Post by findstr » 26 Jul 2021 14:37

Hello!
I have files in a folder:
Listing-01-01.txt
Listing-01-02.txt
Listing-02-01.txt
Listing-02-02.txt
Listing-02-03.txt
Listing-03-01.txt
50 more similar files.
I need to rename them to be:
L01-01.txt
L01-02.txt
L02-01.txt
L02-02.txt
L02-03.txt
L03-01.txt
and do the same for all the other 50 files in the folder.
How can I do it with REN command?
Thanks!

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Shorten file names with REN

#2 Post by aGerman » 26 Jul 2021 14:47

Tokenize the file names in a FOR /F loop.

Code: Select all

pushd "C:\your\folder"
for /f "tokens=1* delims=-" %%i in ('dir /a-d /b "Listing-*.txt"') do ren "%%i-%%j" "L%%j"
popd
Steffen

findstr
Posts: 17
Joined: 09 Jun 2021 12:36

Re: Shorten file names with REN

#3 Post by findstr » 26 Jul 2021 17:03

Thanks!

Post Reply