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!

File Name Manipulation & Rename (same directory)

Status
Not open for further replies.

tourcd

IS-IT--Management
Dec 5, 2005
37
I have 1000’s of files generated every day that are in the format…

“xx_12345678901_ tbdy _garbage.txt”

(yes there are lots of spaces between tbdy and _garbage)

I’d like to end up with…

1234567890_tbdy.txt

So this means removing xx_ and also everything after the tbdy and putting back “.txt”.

The files live and have to remain in the same directory e.g. c:\logs

I’ve modified someone elses example script (from this forum) that happily moves the file from one directory to another but it’s not happy if I make the destination directory the same as the source.

Does anybody have a suggestion on how I might solve my problem above?

Thanks
 
What is YOUR actual code and which error on which line ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Okay, here's my code which I've changed since my original post...

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='C:\Source'} Where " _
& "ResultClass = CIM_DataFile")

For Each objFile In colFileList
strReplace = replace(objFile.FileName, "ch_", "")
StrNewName = objFile.Drive & objFile.Path & _
strReplace & "." & objFile.Extension
errResult = objFile.Rename(strNewName)
Next


My question now is how do I pick out the first space and then get rid of everything after it?
 
Okay, I've now answered my last question, I can now get rid of the spaces and the rest of the garbage after it by using...

strReplace = Left(objFile.FileName,((InStr(objFile.FileName, " ")-1)))

But how do I combine the two??
 
This may help

Option Explicit

Dim RegEx : Set RegEx = New RegExp
RegEx.Pattern = "xx_(\d+_)\s*(\w+).+(\.txt)$"
RegEx.IgnoreCase = True

Dim strTemp : strTemp = "xx_12345678901_ tbdy _garbage.txt"
WScript.Echo RegEx.Replace(strTemp, "$1$2$3")

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Okay, now I've combined the two...

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim strCurrentFolder
strCurrentFolder = "C:\Destination"
Dim strTargetFolder
strTargetFolder = "C:\Destination\Transformed\"
Dim objFolder
Set objFolder = objFSO.GetFolder(strCurrentFolder)

ListDirectory objFolder

Sub ListDirectory(objFolder)
Dim objFile
Dim objSubFolder

For Each objFile In objFolder.Files
If right(objFile.Path, 4) = ".wav" Then
strStripedCH = mid(objFile.Name, 4)
objFSO.CopyFile objFile.Path, strTargetFolder & (left(strStripedCH,((InStr(strStripedCH, " ")-1)))) & ".wav", TRUE
End If
Next
End Sub

When I run this script I get the following error...

Script: c:\source\TestScript08.vbs
Line: 23
Char: 2
Error: Invalid procedure call or argument: 'left'
Code: 800A005
Source: Microsoft VBScript runtime error

Can anybody tell me where I'm going wrong?

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top