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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing file extension and move to new folder

Status
Not open for further replies.

mydisney

Programmer
May 7, 2007
55
US
I have a vb script where I need to change file extension for all files that starts with t* and that have extension = .txt. Here is the code I have but it does not work right (It works fine when I only have .txt but then it picks up more files than I want)

Dim filesys, file, folderName, objFile, folderObj, fileColl, objRegExp, newFile
Set filesys = CreateObject("Scripting.FileSystemObject")

folderName = "c:\myfolder\myftpfolder\"
Set folderObj = filesys.GetFolder(folderName)
Set fileColl = folderObj.Files

Set objRegExp = New RegExp
objRegExp.Pattern = "t*.txt" 'looking for t*.txt (extension) match
objRegExp.IgnoreCase = True

For Each objFile In fileColl
If objRegExp.Test(objFile.Name) Then
newFile = objRegExp.Replace (objFile.Name, ".bak")
filesys.MoveFile objFile, folderName & newFile
End If
Next
 
Try changing your pattern

^t.*\.txt$

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Hi dm4ever:

It did not not work, it did not change extension. Any other suggestion?

Here is the code I used:

objRegExp.Pattern = "^t.*\.txt$ "
 
boy do I long for the days of

Code:
c:\> ren c:\wp\doc\.....*.dat *.bak
!!!!!!!
 
I would take it in pieces. First see if the pattern is only showing files that match the criteria you're looking for. Once you confirm that, then we can start working on the rest.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Echo out or MsgBox the output of this line
newFile = objRegExp.Replace (objFile.Name, ".bak")

I don't think it is changing the extension like you think it might.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
It takes all the files in the folder with .txt extension and renames to .bak. This is good so far except I only want to have files starting with *T renamed.
 
This example shows that the pattern only shows file that start with a "T" and end with .txt

Code:
Option Explicit

Dim arrTemp : arrTemp = Array("test123.txt", "abc1.txt", "sldkrf.ber", "taaer.txt", "tadbs.exe")
Dim RegEx : Set RegEx = New RegExp
RegEx.Pattern = "^t.*\.txt$"
RegEx.IgnoreCase = True
Dim strTemp
For Each strTemp In arrTemp
	If RegEx.Test(strTemp) Then
		WScript.Echo strTemp
	End If
Next

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
[1]>objRegExp.Pattern = "t*.txt" 'looking for t*.txt (extension) match
[tt]objRegExp.Pattern = "^(t)(.*\.)txt$" 'looking for t*.txt (extension) match[/tt]

[2]>newFile = objRegExp.Replace(objFile.Name, ".bak")
[tt]newFile = objRegExp.Replace(objFile.Name, "$1$2bak")[/tt]
 
Hi Guys:

Finally got the code to work. Here it is:

Dim filesys, file, folderName, objFile, folderObj, fileColl, objRegExp, newFile
Set filesys = CreateObject("Scripting.FileSystemObject")

folderName = "c:\travelers\fromtravelers\"
Set folderObj = filesys.GetFolder(folderName)
Set fileColl = folderObj.Files

Set objRegExp = New RegExp
objRegExp.Pattern = "^(t)(.*\.)txt$"
objRegExp.IgnoreCase = True

For Each objFile In fileColl
If objRegExp.Test(objFile.Name) Then
newFile = objRegExp.Replace(objFile.Name, "$1$2bak")
filesys.MoveFile objFile, folderName & newFile
End If
Next

Thank you all for being very supportive and have a great weeek end!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top