Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename Files (slightly odd one though) 1

Status
Not open for further replies.

ncotton

IS-IT--Management
Jan 27, 2006
2,841
GB
Hey guys, looking for some assistance.

This is the scene.

User had files called file1.txt etc and wanted them to be renamed to file1.abc, however when using the command line to change all the files there was an error in the syntax, and now they have a folder full of files called file1abc.txt.

How can I go back and delete the last 7 characters (incorrect addition + . + original ext) and replace it with the correct new extention.

Thanks
NC
 
More of a DOS question so here is a dos solution

Remove the echo on the second-to-last line when you're happy its doing what it should be

@echo off
for %%i in (file*.txt) do (set fname=%%i) & call :rename
goto :eof
:rename
echo ren %fname% %fname:~0,5%.abc
goto :eof



In order to understand recursion, you must first understand recursion.
 
Code:
Dim fso
Set fso = CreateObject("Scripting.Filesystemobject")

For Each file In fso.GetFolder("c:\temp\myfolder").Files

  newpath = file.Path
  newpath = Left(newpath,Len(newpath)-4) 'remove last 4 characters
  newpath = Left(newpath,Len(newpath)-3) & "." & Right(newpath,3) 'insert a dot character before the extension
  
  fso.MoveFile file.path,newpath
Next

Need a good VBScript editor and debugger? Get VbsEdit
 
Alternatively,

Code:
set objFSO = CreateObject("Scripting.FileSystemObject")

for each objFile in objFSO.GetFolder("c:\temp\myfolder").Files
  objFile.Name = replace(objFile.Name, "abc", "")
next

-Geates
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top