|
From: scot on 25 Jun 2008 10:54 Hi: I've written a macro in vba that works well where it will go to a directory, find a file and rename it. The problem is, I can't get it to work in vbs. The catch is that I won't know the exact file name to find. So, for example, the file will begin with "Int_Data" but then the program that creates the file will attach a set of numbers to the end of the file extension like "Int_Data_123456". The numbers are something that I cannot predict. Below is the code I've written and does not work. I think I'll need to use InStr to find the file, but I'm not sure how to use it to find a file beginning with "Int_Data" and rename it. Any suggestions? Thanks. Function Interval_Datafile_Name_Change() Dim fso Dim SourceFile Dim DestFile Set fso = CreateObject("Scripting.FileSystemObject") SourceFile = "C:\Documents and Settings\****\My Documents\Reporting\Data\Int_Data_" & "" DestFile = "C:\Documents and Settings\****\My Documents\Reporting\Data\Int_Data.xls" If fso.FileExists(SourceFile) Then fso.MoveFile SourceFile, DestFile End If Set fso = Nothing End Function
From: McKirahan on 25 Jun 2008 11:50 "scot" <scot(a)discussions.microsoft.com> wrote in message news:AAD0AAF1-55F9-4E8D-989C-2BA6E2BDBE86(a)microsoft.com... > Hi: > > I've written a macro in vba that works well where it will go to a directory, > find a file and rename it. The problem is, I can't get it to work in vbs. > The catch is that I won't know the exact file name to find. So, for example, > the file will begin with "Int_Data" but then the program that creates the > file will attach a set of numbers to the end of the file extension like > "Int_Data_123456". The numbers are something that I cannot predict. > > Below is the code I've written and does not work. I think I'll need to use > InStr to find the file, but I'm not sure how to use it to find a file > beginning with "Int_Data" and rename it. Use FSO's GetFolder method to retrieve a list of filenames in the folder of interest then examne those for the one that meets your criteria. Before renaming it, make sure that the new filename doesn't already exist. Will this help? Watch for word-wrap. Option Explicit '**** '* Rename "cFIL" in "cFOL" to "cFIL" without the wildcard "*". '**** '* Const cFOL = "C:\Documents and Settings\****\My Documents\Reporting\Data\" Const cFIL = "Int_Data*.xls" '* Dim arrFIL arrFIL = Split(cFIL,"*") Dim strFIL Dim strMSG strMSG = cFIL & " file not found!" Dim strNEW strNEW = arrFIL(0) & arrFIL(1) '* Dim objFSO Set objFSO = CreateObject("Scripting.FileSystemObject") Dim objGFO Set objGFO = objFSO.GetFolder(cFOL) For Each strFIL in objGFO.Files If Left(strFIL.Name,Len(arrFIL(0))) = arrFIL(0) _ And Right(strFIL.Name,Len(arrFIL(1))) = arrFIL(1) Then If objFSO.FileExists(cFOL & strNEW) Then strMSG = strNEW & " file already exists!" Else strMSG = strFIL.Name & " file renamed to " & strNEW objFSO.MoveFile cFOL & strFIL.Name, cFOL & strNEW End If Exit For End If Next Set objFSO = Nothing '* MsgBox strMSG,vbInformation,WScript.ScriptName
From: scot on 25 Jun 2008 12:21 "McKirahan" wrote: > "scot" <scot(a)discussions.microsoft.com> wrote in message > news:AAD0AAF1-55F9-4E8D-989C-2BA6E2BDBE86(a)microsoft.com... > > Hi: > > > > I've written a macro in vba that works well where it will go to a > directory, > > find a file and rename it. The problem is, I can't get it to work in vbs. > > The catch is that I won't know the exact file name to find. So, for > example, > > the file will begin with "Int_Data" but then the program that creates the > > file will attach a set of numbers to the end of the file extension like > > "Int_Data_123456". The numbers are something that I cannot predict. > > > > Below is the code I've written and does not work. I think I'll need to > use > > InStr to find the file, but I'm not sure how to use it to find a file > > beginning with "Int_Data" and rename it. > > Use FSO's GetFolder method to retrieve a list of filenames in the > folder of interest then examne those for the one that meets your > criteria. Before renaming it, make sure that the new filename > doesn't already exist. > > Will this help? Watch for word-wrap. > > Option Explicit > '**** > '* Rename "cFIL" in "cFOL" to "cFIL" without the wildcard "*". > '**** > '* > Const cFOL = "C:\Documents and Settings\****\My > Documents\Reporting\Data\" > Const cFIL = "Int_Data*.xls" > '* > Dim arrFIL > arrFIL = Split(cFIL,"*") > Dim strFIL > Dim strMSG > strMSG = cFIL & " file not found!" > Dim strNEW > strNEW = arrFIL(0) & arrFIL(1) > '* > Dim objFSO > Set objFSO = CreateObject("Scripting.FileSystemObject") > Dim objGFO > Set objGFO = objFSO.GetFolder(cFOL) > For Each strFIL in objGFO.Files > If Left(strFIL.Name,Len(arrFIL(0))) = arrFIL(0) _ > And Right(strFIL.Name,Len(arrFIL(1))) = arrFIL(1) Then > If objFSO.FileExists(cFOL & strNEW) Then > strMSG = strNEW & " file already exists!" > Else > strMSG = strFIL.Name & " file renamed to " & strNEW > objFSO.MoveFile cFOL & strFIL.Name, cFOL & strNEW > End If > Exit For > End If > Next > Set objFSO = Nothing > '* > MsgBox strMSG,vbInformation,WScript.ScriptName > > > McKirahan: Thanks for the reply. I'm not too familiar with the Const expression, so I apologize if this is my error, but when I run the code I get an error on cFil stating that a constant expression is required. Scot
From: McKirahan on 25 Jun 2008 14:45 "scot" <scot(a)discussions.microsoft.com> wrote in message news:14133271-E8C4-44C5-BBAD-F939DCA8EFC1(a)microsoft.com... > > Const cFIL = "Int_Data*.xls" > Thanks for the reply. I'm not too familiar with the Const expression, so I > apologize if this is my error, but when I run the code I get an error on cFil > stating that a constant expression is required. "Const" stands for "constant" -- a variable that doesn't change. Equivalent to: Dim s s = value What value did you use? Did you try to change it -- you can't. Did you use an asterisk (wildcard) to denote the variable filename? Post your version of the code if necessary. Mine worked "as-is".
From: scot on 25 Jun 2008 15:35 McKirahan: After reading your post, I went ahead and re-copied and re-pasted your script and it worked fine. I'm not sure what I did, but it's all good now. Thanks for your help. Scot
|
Pages: 1 Prev: How to mail enable an Distributiongroup Next: update text file |