Rename with a batch file - but only the files ending with *D

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jmi
Posts: 8
Joined: 06 Jan 2014 07:52

Rename with a batch file - but only the files ending with *D

#1 Post by jmi » 06 Jan 2014 08:05

Hi
I want to rename all the files in the directory "C:\temp\TEST\IN\" but only txt-files that end with a "D".

Here is what I tried in a batch... but both didn't work. Help highly appreciated :)

-----try1-----
@echo off
set Quelle="C:\temp\TEST\IN\"
Set DateiFilter="*D.txt"
for /r %Quelle% %%i in (%DateiFilter%) do rename %%i %%~niI.txt
end

-----try2-----
@echo on
setlocal enableDelayedExpansion
set Quelle="C:\temp\TEST\IN\"
Set DateiFilter="*.txt"
set y="D"
set z="I"
for /r %Quelle% %%i in (%DateiFilter%) do (set x=%%i rename "%%i" "!x:D.txt=I.txt!")
pause
end

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Rename with a batch file - but only the files ending wit

#2 Post by foxidrive » 06 Jan 2014 08:25

This should help. Remove the echo to enable the renaming.

Code: Select all

@echo off
pushd "C:\temp\TEST\IN\"
for /f "delims=" %%a in ('dir "*D.txt" /b /a-d ') do echo ren "%%a" "%%~naI%%~xa"
popd
pause

jmi
Posts: 8
Joined: 06 Jan 2014 07:52

Re: Rename with a batch file - but only the files ending wit

#3 Post by jmi » 06 Jan 2014 09:14

Thank you foxidrive!
This does rename it with adding a "I" correctly to the files with a D at the end.
How can I REPLACE the D

1234D.txt --> 1234I.txt (D gets replaced by I)
1234X.txt --> 1234X.txt (stays the same)

THANK YOU :)

jmi
Posts: 8
Joined: 06 Jan 2014 07:52

Re: Rename with a batch file - but only the files ending wit

#4 Post by jmi » 06 Jan 2014 09:18

Thank you foxidrive!
This does rename it with adding a "I" correctly to the files with a D at the end.
But how can I REPLACE the D?

1234D.txt --> 1234I.txt (D gets replaced by I)
1234X.txt --> 1234X.txt (stays the same)

THANK YOU :)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Rename with a batch file - but only the files ending wit

#5 Post by foxidrive » 06 Jan 2014 09:24

Do you want every D to be replaced?

jmi
Posts: 8
Joined: 06 Jan 2014 07:52

Re: Rename with a batch file - but only the files ending wit

#6 Post by jmi » 06 Jan 2014 09:25

No, only the last one, the one infront of the .txt

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Rename with a batch file - but only the files ending wit

#7 Post by foxidrive » 06 Jan 2014 09:30

I wasn't very clear, Will there be other D's in the filename?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Rename with a batch file - but only the files ending wit

#8 Post by foxidrive » 06 Jan 2014 09:34

This solution can fail if the filenames contain ! characters.

Remove the echo to enable the rename.

Code: Select all

@echo off
pushd "C:\temp\TEST\IN\"
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir "*D.txt" /b /a-d ') do (
   set "name=%%~na"
   set "name=!name:~0,-1!I"
   echo ren "%%a" "!name!%%~xa"
)
popd
pause

jmi
Posts: 8
Joined: 06 Jan 2014 07:52

Re: Rename with a batch file - but only the files ending wit

#9 Post by jmi » 06 Jan 2014 09:35

No, there will be no other D in the filename.

jmi
Posts: 8
Joined: 06 Jan 2014 07:52

Re: Rename with a batch file - but only the files ending wit

#10 Post by jmi » 06 Jan 2014 09:39

The file-names look like this:
20131009-17-02-01200278D.txt
or
20131009-17-02-01200278.txt
But never contain letters, only numbers or a D at the end...

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Rename with a batch file - but only the files ending wit

#11 Post by Squashman » 06 Jan 2014 09:46

jmi wrote:The file-names look like this:
20131009-17-02-01200278D.txt
or
20131009-17-02-01200278.txt
But never contain letters, only numbers or a D at the end...

That looks similar to what we do with our file naming conventions.
We name a lot of things with a date and time stamp but some files get named with a Date stamp and then a letter that corresponds to the 24 hour clock. Those are for files we only send out once a day to clients.

jmi
Posts: 8
Joined: 06 Jan 2014 07:52

Re: Rename with a batch file - but only the files ending wit

#12 Post by jmi » 06 Jan 2014 09:50

PERFECT foxidrive!!! HERO of the day ;)
Thank you very very much! Your last post did the job!

jmi
Posts: 8
Joined: 06 Jan 2014 07:52

Re: Rename with a batch file - but only the files ending wit

#13 Post by jmi » 06 Jan 2014 09:52

I just needed to add the end bracket:

@echo off
pushd "C:\temp\TEST\IN\"
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir "*D.txt" /b /a-d ') do (
set "name=%%~na"
set "name=!name:~0,-1!I"
ren "%%a" "!name!%%~xa")
popd
pause

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Rename with a batch file - but only the files ending wit

#14 Post by foxidrive » 06 Jan 2014 10:04

Well done. I'm glad it helps.

Post Reply