Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I need to replace part of a file name using a command line utility.
I have a directory full of files all prefixed with a name that I want to change, unfortunately the name i want to change to is longer than the original name so
rename oldname*.txt longname*.txt
changes
oldname@file.txt to longnamefile.txt
note the missing @ from the name e.g. it should gave been longname@file.txtany ideas?

Moin Lee,
this way it will work:
ren DriveLetter:\path\???@file.txt verylong@file.txt
where you must type exactly as many questionmarks as the old filenames' identical part has got spells.
mfg, Carl.

Thanks guys....
I needed to do this with an environment variable.... I came across this on a scripting site and it works...
for %%f in (*.txt) do call :RENAME %%f
:RENAME
echo Called with %1
for /f "eol=; tokens=1* delims=@" %%I in ("%1") do rename %1 %env_variable%@%%JSo job done thanks.
Carl thanks - I would have needed to know how many chr in the original file - is there a length function in batch files so I could work out how many ???? to use? I'm doing this on multiple machines with multiple users. Basically Im trying to migrate the users cookies in a login script without asking the user to do anything.

The "problem" with the original code is that it does exactly what you asked: @ is part of oldname and you did not include it in longname.
Instead of ren DriveLetter:\path\???@file.txt verylong@file.txt
Why not ren DriveLetter:\path\*@file.txt verylong@file.txt
That way it will change any size oldname file withoput having to count "?".

Hi
"Why not ren DriveLetter:\path\*@file.txt verylong@file.txt"
I'm trying to rename every instance of oldname@something.txt not one instance so I need a wildcard in both parts of the command.
your command would rename every file to verylong@file.txt , well it would try to but fail after the first instance.
what i have is lots of files:
oldname@bt.com.txt
oldname@microsoft.com.txt
oldname@citrix.com.txtand I want to rename them all to
newname@bt.com.txt
newname@microsoft.com.txt
newname@citrix.com.txtThanks for the help guys... but I have a solution in my previous post now using a for command.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |