Operating System - Microsoft
1752790 Members
6154 Online
108789 Solutions
New Discussion юеВ

Re: Rename file in DOS script (batch)

 
SOLVED
Go to solution
Kenn Chen
Advisor

Rename file in DOS script (batch)

Currently, I want to write a batch file to rename my files in one directory. E.g. directory contents 3 files (123ab, 123bc, 123cd) and i have to rename this file to 123ab.dat, 123bc.dat and 123cd.dat respectively. Just add in extentsion .DAT. I can't use ren 123* 123*.dat. So pelase help ??
Cyber Zen
6 REPLIES 6
Enes Dizdarevic
Honored Contributor

Re: Rename file in DOS script (batch)

You can use this one:
for %f in (123*) do ren %f %f.dat
It works even from command line.
Enes Dizdarevic
Honored Contributor

Re: Rename file in DOS script (batch)

I forgot to say: Previous example works only from command line. In batch scripts you have to replace % with %% ie:
for %%f in (123*) do ren %%f %%f.dat
Be careful with this, otherwise you can get 123ab.dat.dat.dat.dat
Kenn Chen
Advisor

Re: Rename file in DOS script (batch)

Yep, your script is work but my filename is long and if i use the batch file to renmae the filename with extention .DAT the whole file name has been changed. E.g. my filename is MY12345keying and need to change to MY12345keying.dat but the dos show filename as MY123~1 and when i issue the batch file it will change to MY123~1.dat instead of its actual filename. Please advice. Thanks.
Cyber Zen
Enes Dizdarevic
Honored Contributor
Solution

Re: Rename file in DOS script (batch)

What is your OS. I tried this on Windows2000 (SP2) and it works with long file names.

Note: to avoid change of already changed names when you reapply the script change (123*) to (123*.).
Kenn Chen
Advisor

Re: Rename file in DOS script (batch)

MY PC is Win98 second edition. My filename is long but after converted then become MY~1.DAT
i have to change this to full name. Please help.
Cyber Zen
Enes Dizdarevic
Honored Contributor

Re: Rename file in DOS script (batch)

I think that the problem is in fact that Win98 command prompt is actualy DOS command prompt and works with short file names only. If your PC is network connected and you have win2000 or nt machine on the network try executing script from these machines. If not you will have to find some third party scripting tool which works with long file names on win98 an write your script using this tool.