# Solved: Rename beginning of a filename from batch or command prompt



## sososm (Nov 30, 2010)

Hy everyone, I want to rename a file from command prompt or batch file, for example:

new_55555_888_66666.txt
to
old_55555_888_66666.txt

after the 4th character, the filename must remain the same and the number of characters and the characters itself toward end of filename differs from file to file.
If someone can help me with the syntax or another method, I'll appreciate it.
Thx for help and your time in advance.


----------



## TheOutcaste (Aug 8, 2007)

Welcome to TSG!
This should do it.

```
Ren "new*.txt" "old*.txt"
```


----------



## sososm (Nov 30, 2010)

Thanx for your prompt reply;
That does't work, it only renames the first 4 chars > result is old_.txt, the rest of the filename is missing.

I just found something that worked for me:

@Echo Off & Setlocal enableextensions enabledelayedexpansion
FOR /F "tokens=* delims=" %%A IN ('dir /a-d /b C:\logs\*.txt') DO (
SET _tmpstr=%%A
SET _tmpstr=!_tmpstr:new=old!
rename %%A !_tmpstr!
)

This replaces the chars "new" to "old", the rest of the filename remains unchanged.
Thx alot.


----------



## TheOutcaste (Aug 8, 2007)

That's odd, what OS are you running? It works for me in both XP and Win 7:

```
C:\test>dir
 Volume in drive C has no label.
 Volume Serial Number is 4448-2915

 Directory of C:\test

11/30/2010  03:14 PM    <DIR>          .
11/30/2010  03:14 PM    <DIR>          ..
11/30/2010  03:14 PM                 2 new_55555_888_66666.txt
               1 File(s)              2 bytes
               2 Dir(s)  18,730,164,224 bytes free

C:\test>ren new*.txt old*.txt

C:\test>dir
 Volume in drive C has no label.
 Volume Serial Number is 4448-2915

 Directory of C:\test

11/30/2010  03:15 PM    <DIR>          .
11/30/2010  03:15 PM    <DIR>          ..
11/30/2010  03:14 PM                 2 old_55555_888_66666.txt
               1 File(s)              2 bytes
               2 Dir(s)  18,730,164,224 bytes free

C:\test>
```


----------



## sososm (Nov 30, 2010)

You're right, it's working, I've also tried:

C:\logs>dir
Volume in drive C has no label.
Volume Serial Number is 68BC-7CE3

Directory of C:\logs

01.12.2010 11:41 .
01.12.2010 11:41 ..
01.12.2010 11:41 0 new_55555_888_66666.txt
1 File(s) 0 bytes
2 Dir(s) 20.684.140.544 bytes free

C:\logs>ren new*.txt old*.txt

C:\logs>dir
Volume in drive C has no label.
Volume Serial Number is 68BC-7CE3

Directory of C:\logs

01.12.2010 11:50 .
01.12.2010 11:50 ..
01.12.2010 11:41 0 old_55555_888_66666.txt
1 File(s) 0 bytes
2 Dir(s) 20.684.001.280 bytes free
---------------------------------------------------------------------------------------------
but it's not working in other scenario like:

C:\logs>dir
Volume in drive C has no label.
Volume Serial Number is 68BC-7CE3

Directory of C:\logs

01.12.2010 11:58 .
01.12.2010 11:58 ..
01.12.2010 11:41 0 new_20100918_362_745325.txt
1 File(s) 0 bytes
2 Dir(s) 20.683.833.344 bytes free

C:\logs>ren new*.txt old-gen.log*.txt

C:\logs>dir
Volume in drive C has no label.
Volume Serial Number is 68BC-7CE3

Directory of C:\logs

01.12.2010 11:59 .
01.12.2010 11:59 ..
01.12.2010 11:41 0 old-gen.log.txt
1 File(s) 0 bytes
2 Dir(s) 20.683.833.344 bytes free
---------------------------------------------------------------------
If exists a working combination of wildcards in this case please reply.
Thx.


----------



## TheOutcaste (Aug 8, 2007)

It's the period that's preventing the 2nd one from working.
The first period is seen as the end of the file name and the start of the extension, and clears the wildcard value, even though it's actually part of the file name. By the time Windows sees that there is another period, it's too late, the value has been cleared. Replace it with another character.
Plus you can't change the length of the file name. The total of *new*.txt* has to be the same as *old-gen-log***.txt*

For example
*ren new*.txt old-gen-log*.txt*
Will replace *new* with *old*, then replaces the next 8 characters with *-gen-log*, then puts in the rest of the original value

*new_20100918_362_745325.txt
old-gen-log8_362_745325.txt*

If you need to change the length, or add a period, you need to use substring substitution:
*SET _tmpstr=%_tmpstr:new=old-gen.log%*
Or if delayed expansion is needed:
*SET _tmpstr=!_tmpstr:new=old-gen.log!*


----------



## sososm (Nov 30, 2010)

Well, everything is ok for now, but I have another problem.
In the filename *frg.gen.log_20101215183211_456_6-2.log*, I want to insert a "-" between date chars and "." between time chars to look something like:
frg.gen.log_2010-12-15_18.32.11_456_6-2.log.
The lenght of filename is variable of course so I want to insert the characters regardless of length of the filename, something like... to set a delim from 2010xxxxxxxxxx which is unchanged, and for next 10 chars insert a "-" after first 2, then after next two
so on.
I don't know how to use FOR command with this.


----------



## TheOutcaste (Aug 8, 2007)

```
@Echo Off
SetLocal EnableDelayedExpansion
PushD C:\Test
For /F "Tokens=1,2* Delims=_" %%I In ('Dir /A-D /B') Do (
Set _T1=%%J
Set _T1=!_T1:~0,4!-!_T1:~4,2!-!_T1:~6,2!_!_T1:~8,2!.!_T1:~10,2!.!_T1:~12,2!
Ren "%%I_%%J_%%K" "%%I_!_T1!_%%K"
)
PopD
```
This assumes that the *_* will be the first one in the file name and is immediately before the date/time portion, which will always be formatted as YYYYMMDDHHMMSS*_* or YYYYDDMMHHMMSS*_*.
If the *_* after the date/time portion is missing, the rest of the file name will be removed.

*Random_20101215183211_stuffhere.any*

Doesn't matter what the other parts of the file name are, except *!* can't be part of the file name. If there is one, the file name will not be change and will display an error. If there are two, everything between them will be removed.


----------



## sososm (Nov 30, 2010)

THANK YOU VERY VERY MUCH!!!!!!!!!!!

Works like a charm, this saves a lot of time. :up:

TheOutcaste I wish you a very merry christmas and a happy new year


----------

