Hello everyone,

I am trying to find a way to rename files that get loaded onto a server
every night based upon other files that get uploaded with them.

For example some of the files we get are as follows -

r29-dcom.txt, arr56r29.xfr
r15-dcom.txt, arr22r15.xfr

Now I need to rename the r29-dcom.txt and r15-dcom.txt files to have
the number that the XFR files has. So after renaming them I would end
up with the following.

r29-dcom56.txt, arr56r29.xfr
r15-dcom22.txt, arr22r15.xfr

There are quite a few files that I have to do this for on an on going
basis and I need a way to speed it. The big issue is that the number
in the arr56r29.xfr will increment everyday until it reaches 00 and
then start again, so tomorrow would be arr57r29.xfr.

If anyone can offer any advice on how to achieve this that would be
great.

Re: VBScript to Rename Files by deckhopper

deckhopper
Mon Jan 02 11:29:45 CST 2006

Here's a script that may work for you. Test it out using a sacrificial
folder first. This was the best I could do given the limited amount of
information you have here. If you need more tweaking of this script,
reply back or tinker around with the script.

This script assumes the following:
1. You have a folder on the desktop (under the administrator) account
called "VBTests"
2. Change the variable "fileQueryPath" to suit your needs.
3. Keep the "\\" in the variable "fileQueryPath" and do NOT insert
drive letter. Otherwise, it will not work

'############
'Begin Script
'############################################################################
Option Explicit

Dim objFSO, objWMIService, objFile, colFiles
Dim arrFileNumber()
Dim x, y, strComputer, fileQueryPath, longName, shortName, extNameOnly
Dim newName

x = 0
y = 0
strComputer = "."

fileQueryPath = "\\documents and
settings\\administrator\\desktop\\VBTests\\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")


findFilesInFolder

Set objFSO = Nothing
Set objWMIService = Nothing
Set colFiles = Nothing


'******************************************************************************
Sub findFilesInFolder

Set colFiles = objWMIService.ExecQuery _
("SELECT * FROM CIM_DataFile WHERE Path = '" & fileQueryPath & "'")

For Each objFile in colFiles

longName = objFile.Name
shortName = objFSO.GetFileName(longName)
extNameOnly = objFSO.GetExtensionName(shortName)

If extNameOnly = "xfr" Then
ReDim Preserve arrFileNumber(x)
arrFileNumber(x) = Mid(shortName,4,2)
End If

If extNameOnly = "txt" Then
newName = Left(shortName,8) & arrFileNumber(y) & ".txt"
MsgBox newName & vbCr & vbCr & longName
MoveAFile longName, newName
y = y + 1
End If

x = x + 1
Next

End Sub
'*******************************************************************|


'******************************************************************************
Sub MoveAFile(Drivespec, name)

objFSO.MoveFile Drivespec, _
"C:\Documents and Settings\Administrator\Desktop\VBTests\" & name

End Sub
'*******************************************************************|

'#############################################################################
'End Script
'###########

tim_wallace@games-workshop.com.au wrote:
> Hello everyone,
>
> I am trying to find a way to rename files that get loaded onto a server
> every night based upon other files that get uploaded with them.
>
> For example some of the files we get are as follows -
>
> r29-dcom.txt, arr56r29.xfr
> r15-dcom.txt, arr22r15.xfr
>
> Now I need to rename the r29-dcom.txt and r15-dcom.txt files to have
> the number that the XFR files has. So after renaming them I would end
> up with the following.
>
> r29-dcom56.txt, arr56r29.xfr
> r15-dcom22.txt, arr22r15.xfr
>
> There are quite a few files that I have to do this for on an on going
> basis and I need a way to speed it. The big issue is that the number
> in the arr56r29.xfr will increment everyday until it reaches 00 and
> then start again, so tomorrow would be arr57r29.xfr.
>
> If anyone can offer any advice on how to achieve this that would be
> great.


Re: VBScript to Rename Files by ekkehard

ekkehard
Mon Jan 02 12:40:31 CST 2006

deckhopper wrote:

> Here's a script that may work for you. Test it out using a sacrificial
> folder first. This was the best I could do given the limited amount of
> information you have here. If you need more tweaking of this script,
> reply back or tinker around with the script.
[...]

> For Each objFile in colFiles
>
> longName = objFile.Name
> shortName = objFSO.GetFileName(longName)
> extNameOnly = objFSO.GetExtensionName(shortName)
>
> If extNameOnly = "xfr" Then
> ReDim Preserve arrFileNumber(x)
> arrFileNumber(x) = Mid(shortName,4,2)
> End If
>
> If extNameOnly = "txt" Then
> newName = Left(shortName,8) & arrFileNumber(y) & ".txt"
> MsgBox newName & vbCr & vbCr & longName
> MoveAFile longName, newName
> y = y + 1
> End If
>
> x = x + 1
> Next
[...]

> tim_wallace@games-workshop.com.au wrote:
>
>>Hello everyone,
>>
>>I am trying to find a way to rename files that get loaded onto a server
[...]
>>For example some of the files we get are as follows -
>>
>>r29-dcom.txt, arr56r29.xfr
>>r15-dcom.txt, arr22r15.xfr
>>
>>Now I need to rename the r29-dcom.txt and r15-dcom.txt files to have
>>the number that the XFR files has. So after renaming them I would end
>>up with the following.
>>
>>r29-dcom56.txt, arr56r29.xfr
>>r15-dcom22.txt, arr22r15.xfr
[...]
Hi deckhopper,

I reply to your message because Tim and I (ehvbs) are discussing
Tim's problem at
http://www.visualbasicscript.com/m_29213/tm.htm
and I think it wouldn't be fair if you were not aware of this fact.

Because of this discussion I tested your script and learned something
new (for me) about using WMI to access the filesystem. Thank you for
that.

There is just one problem: I can't see how your code keeps track
of the paired files to make sure that eg r29-dcom.txt is renamed
according to arr56r29.xfr (and not arr22r15.xfr). I tried to
comprehend your usage of x and y, but I need your help/explanation
to understand it.

Re: VBScript to Rename Files by deckhopper

deckhopper
Mon Jan 02 13:38:28 CST 2006

Well, I made an assumption (perhaps a wrong assumption) about the
naming of the files. In his example, the files are chronologically
paired --> r15 comes before r29 and arr22 comes before arr56 (r15 has
to be tied to arr22 and r29 has to be tied to arr56). Because the For
Loop spins through the files in chronological fashion, the files are
paired up using separate parameters (x and y).

In essence, think of x and y as the left and right side of a zipper.
Because the file names are tied together incrementally (my assumption),
x is the left part of the zipper, and y is the right part of the
zipper. Using the method above, I'm "tying" the left and right sides
together.

If my assumption is wrong, then the script can be tweaked using the
"InStr" function:
1. Roll through the .xfr files, pull the number variable out of each
one and put it into arrNumber(x), at the same time, pull the full file
name and put it into arrFileName(x).
2. Then roll through the .txt files and compare the .txt file (using
the InStr function) with the dynamic array "arrFileName(y)", if their
numbers match, do the following:

a. Pass the arrNumber(y) variable to MoveFile (rename) it.


r29-dcom.txt, arr56r29.xfr
r15-dcom.txt, arr22r15.xfr