Using the rename command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Gunner
Posts: 7
Joined: 13 Dec 2019 02:40

Using the rename command

#1 Post by Gunner » 13 Dec 2019 03:03

Hi
I have a file named Encoder_TestHarnessLog_19_12_13-083023.xml that I want to rename Test1_19_12_13-083023.xml from within a batch file
but cant see to Achieve this with the rename command.
Please can someone suggest a way of doing this?

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Using the rename command

#2 Post by penpen » 13 Dec 2019 05:38

The following code might help you (although i suspect that you didn't describe the whole issue):

Code: Select all

rename "Encoder_TestHarnessLog_19_12_13-083023.xml" "Test1_19_12_13-083023.xml"
penpen

Gunner
Posts: 7
Joined: 13 Dec 2019 02:40

Re: Using the rename command

#3 Post by Gunner » 13 Dec 2019 05:43

HI thanks
you are correct
I need to retain the date stamp ant the on the end of the original and include it it the second

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

Re: Using the rename command

#4 Post by aGerman » 13 Dec 2019 08:15

In this case you should provide a real example how it looks like before and after renaming (e.g. the number of underscores could be critical here), plus the rules of how to get from the old name to the new (e.g. does the new name always begin with "Test1_").

Steffen

Gunner
Posts: 7
Joined: 13 Dec 2019 02:40

Re: Using the rename command

#5 Post by Gunner » 16 Dec 2019 02:08

Ok let me clarify
Given a file name of

Encoder_TestHarnessLog_19_12_13-113027.xml

I want to generate a filename of

Test1_19_12_13-113027.xml

Where Test1 can be any string (ultimately a command line argument to a batch script

I hope this is clearer

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

Re: Using the rename command

#6 Post by aGerman » 16 Dec 2019 03:17

This code only displays the RENAME command line. Remove ECHO and PAUSE to make it rename the file.

Code: Select all

@echo off &setlocal DisableDelayedExpansion
set "file=Encoder_TestHarnessLog_19_12_13-083023.xml"
set "prefix=Test1"

for %%i in ("%file%") do (
  for /f "tokens=2* delims=_" %%j in ("%%~nxi") do (
    ECHO rename "%%~fi" "%prefix%_%%k"
  )
)
PAUSE
Note: According to your example file name and because you didn't comment to my remark that the number of underscores is critical, I assumed that the file names always have 2 underscores in front of the date.

Steffen

Gunner
Posts: 7
Joined: 13 Dec 2019 02:40

Re: Using the rename command

#7 Post by Gunner » 16 Dec 2019 04:19

Thanks - this is what I was looking for

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

Re: Using the rename command

#8 Post by aGerman » 16 Dec 2019 05:07

I updated the code. There have been two useless variables.

Steffen

Gunner
Posts: 7
Joined: 13 Dec 2019 02:40

Re: Using the rename command

#9 Post by Gunner » 16 Dec 2019 05:16

Thanks
How do I close the question?

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

Re: Using the rename command

#10 Post by aGerman » 16 Dec 2019 05:39

It's not intended to close a topic. If you want to mark your question as solved you may edit your initial post and prepend something like [solved] to the subject.

Steffen

Post Reply