File Extension change

Roberto Madro R.
Roberto Madro R. used Ask the Experts™
on
This is suppose to be straightforward but it's not, I need to change the file extension using a batch job on files with .txt extension to .bak, what the simplest way.

Thx
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
All .TXT files?
Roberto Madro R.Programmer Analyst

Author

Commented:
Yes,

And that's my issue, I need to retain the name(s) of the file(s) but only change their extension.
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
At a command prompt you can just do this from that folder:

REN *.TXT *.BAK

~bp
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Bradley FoxLAN/WAN Systems Administrator
Commented:
I would use vbscript for this.  Save the code below to a .vbs and change "C:\MyFolder\" to the folder you want to work from.

Option Explicit
Dim fso, strFldr, colFiles, objFile

strFldr = "C:\MyFolder\"
Set fso = CreateObject("Scripting.FileSystemObject")

Set colFiles = fso.GetFolder(strFldr).Files

For Each objFile In colFiles
	If Eval ("txt" = fso.GetExtensionName(objFile)) Then
		fso.MoveFile objFile.path, Left(objFile.path,Len(objFile.path) - 3) & "bak"
	End If
Next

Open in new window

Roberto Madro R.Programmer Analyst

Author

Commented:
NO, "Ren" expects a literal in the second half of the command, "*. etc." works well with Delete, Copy, etc., not with "Ren"

Commented:
ren *.txt *.bak will work if they are all in one folder.
Roberto Madro R.Programmer Analyst

Author

Commented:
How do I invoke this?
Roberto Madro R.Programmer Analyst

Author

Commented:
How do I call up this vbscript?
Bradley FoxLAN/WAN Systems Administrator

Commented:
Open Notepad and paste the code in then save it with a .vbs extension.  To call it just double click it.

You can call it from the command line too

cscript.exe C:\Path\To\Script.vbs

Open in new window

Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
The command I suggested will certainly work fine, here's an example.

C:\Temp\ee>dir
 Volume in drive C is Windows 7
 Volume Serial Number is 0000-0000

 Directory of C:\Temp\ee

12/27/2012  01:17 PM    <DIR>          .
12/27/2012  01:17 PM    <DIR>          ..
12/27/2012  01:17 PM                 2 1.txt
12/27/2012  01:17 PM                 2 2.txt
12/27/2012  01:17 PM                 2 3.txt
               3 File(s)              6 bytes
               2 Dir(s)   4,508,450,816 bytes free

C:\Temp\ee>ren *.txt *.bak

C:\Temp\ee>dir
 Volume in drive C is Windows 7
 Volume Serial Number is 0000-0000

 Directory of C:\Temp\ee

12/27/2012  01:17 PM    <DIR>          .
12/27/2012  01:17 PM    <DIR>          ..
12/27/2012  01:17 PM                 2 1.bak
12/27/2012  01:17 PM                 2 2.bak
12/27/2012  01:17 PM                 2 3.bak
               3 File(s)              6 bytes
               2 Dir(s)   4,508,454,912 bytes free

Open in new window

~bp
Roberto Madro R.Programmer Analyst

Author

Commented:
Thanks, it worked,

Can I call a .vbs from a batch file (I'm thinking I should be able to), what do you think?

Thx
Bradley FoxLAN/WAN Systems Administrator
Commented:
sure can just call it like this in your batch file.

cscript.exe C:\Path\To\Script.vbs

Open in new window

Test your restores, not your backups...
Top Expert 2016
Commented:
And this will even work at a DOS command line if you don't want to run it from the actual folder:

ren "c:\temp\ee\*.txt" *.bak

I don't understand why you would want to call a VBS script to do this one line DOS command, from a BAT file.  Am I missing something?

~bp
Roberto Madro R.Programmer Analyst

Author

Commented:
In good hands with EE, Thanks to both of you "mcsween" & "billprew".

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial