Page 1 of 1

Using the rename command

Posted: 13 Dec 2019 03:03
by Gunner
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?

Re: Using the rename command

Posted: 13 Dec 2019 05:38
by penpen
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

Re: Using the rename command

Posted: 13 Dec 2019 05:43
by Gunner
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

Re: Using the rename command

Posted: 13 Dec 2019 08:15
by aGerman
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

Re: Using the rename command

Posted: 16 Dec 2019 02:08
by Gunner
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

Re: Using the rename command

Posted: 16 Dec 2019 03:17
by aGerman
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

Re: Using the rename command

Posted: 16 Dec 2019 04:19
by Gunner
Thanks - this is what I was looking for

Re: Using the rename command

Posted: 16 Dec 2019 05:07
by aGerman
I updated the code. There have been two useless variables.

Steffen

Re: Using the rename command

Posted: 16 Dec 2019 05:16
by Gunner
Thanks
How do I close the question?

Re: Using the rename command

Posted: 16 Dec 2019 05:39
by aGerman
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