kabucek Posted March 17, 2008 Posted March 17, 2008 hi all,i have the following script,and I want to change the name of destination file to something.txt,how to do it?N=NowSet objFSO = CreateObject("Scripting.FileSystemObject")strFolder="L:\"strNewDest = "Q:\"Set objFolder = objFSO.GetFolder(strFolder)For Each strFiles In objFolder.Files If DateDiff("d",N, strFiles.DateLastModified) = -1 Then objFSO.CopyFile strFiles , strNewDest & "\" & strFiles.Name End IfNextthanks
Yzöwl Posted March 17, 2008 Posted March 17, 2008 It sounds to me as if you are trying to use code which does something specific to do something else. The code you've provided copies all files from L: to Q: it doesn't perform a move and rename on a single file.Could you please tell us specifically what it is you want to do and perhaps a more appropriate script could be provided.
kabucek Posted March 17, 2008 Author Posted March 17, 2008 I want to search for latest file with .log extension and then copy that file to new destination with new filenamei have couple scripts and i'm trying to do in many ways but maybe you will find better one.this will find newest file, that is ok but how to copy that file instead of echo it ?thanksSet objFSO = CreateObject("Scripting.FileSystemObject")Set oFolder = objFSO.GetFolder("L:\")seedDate = CDate("1/1/1970")For Each oFile In oFolder.Files If Right(oFile.Name,3) = "log" Then If oFile.DateLastModified > seedDate Then NewestFile = oFile.Name seedDate = oFile.DateLastModified End If End IfNextSub CreateReportFile'Creates or overwrites the report file in the directory of this scriptSet objReportFile = objFSO.CreateTextFile("report.TXT", True)Call WriteReportLine("Report created: " & Date & " at " & Time, 0)Call WriteReportLine("*Turn off word-wrap to view cleanly" & vbCrLf, 0) Call WriteReportLine("Starting Left Directory - " & objLeftDir, 0)Call WriteReportLine("Starting Right Directory - " & objRightDir, 0)End SubWScript.Echo NewestFile
gunsmokingman Posted March 17, 2008 Posted March 17, 2008 (edited) Try this Dim N, NewName, objFolder, objFSO, strFolder, strNewDest, V1 N=Now strFolder="L:\" strNewDest = "Q:\" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFolder = objFSO.GetFolder(strFolder) For Each strFiles In objFolder.Files If InStr(strFiles.Path,".log") Or InStr(strFiles.Path,".Log") Or InStr(strFiles.Path,".LOG") Then If DateDiff("d",N, strFiles.DateLastModified) = -1 Then '-> Split The Name To Get 2 Parts NewName= Split(strFiles.Name,".")'-> Replace The First NewName With Whatever You Want V1 = Replace(NewName(0),"Whatever")'-> Copy The File To It New Name objFSO.CopyFile strFiles , strNewDest & "\" & V1 & "." NewName(1), True End If End If NextThis uses the split and replace methods. The Split makes a 2 element arrayThen you replace the first array element with the name you want.Then you copy the new name plus "." and the second array element. Edited March 17, 2008 by gunsmokingman
kabucek Posted March 19, 2008 Author Posted March 19, 2008 i've tried this:Dim N, NewName, objFolder, objFSO, strFolder, strNewDest, V1N=NowstrFolder="L:\"strNewDest = "Q:\" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFolder = objFSO.GetFolder(strFolder) For Each strFiles In objFolder.Files If InStr(strFiles.Path,".log") Or InStr(strFiles.Path,".Log") Or InStr(strFiles.Path,".LOG") Then If DateDiff("d",N, strFiles.DateLastModified) = -1 Then'-> Split The Name To Get 2 Parts NewName= Split(strFiles.Name,".")'-> Replace The First NewName V1 = Replace(NewName(0),"bobo")'-> Copy The File To It New Name objFSO.CopyFile strFiles , strNewDest & "\" & V1 & "." NewName(1), True End If End If Nextthen i got this :Line: 15char: 62Expected end of statementnot sure what next..thanks
gunsmokingman Posted March 19, 2008 Posted March 19, 2008 (edited) Change this '-> Copy The File To It New NameobjFSO.CopyFile strFiles , strNewDest & "\" & V1 & "." NewName(1), TrueTo this'-> Copy The File To It New NameobjFSO.CopyFile strFiles , strNewDest & "\" & V1 & "." & NewName(1), TrueWhen I wrote this up I forgot the & before the NewName(1) in the line.Edit you could also have the line look like this'-> Copy The File To It New NameobjFSO.CopyFile strFiles , strNewDest & "\" & V1 & ".log", TrueEditI made a change in the script, I added a counter to it. The number get added to the name, this will prevent you from only ending up with 1 file in the destination folder.Option Explicit Dim C1, N, NewName, objFolder, objFSO, strFolder, strNewDest, V1 N=Now strFolder="L:\" strNewDest = "Q:\" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFolder = objFSO.GetFolder(strFolder) For Each strFiles In objFolder.Files If InStr(strFiles.Path,".log") Or _ InStr(strFiles.Path,".Log") Or _ InStr(strFiles.Path,".LOG") Then If DateDiff("d",N, strFiles.DateLastModified) = -1 Then C1 = C1 + 1 If Len(C1) = 1 Then C1 = "00" & C1 If Len(C1) = 2 Then C1 = "0" & C1 If Len(C1) = 3 Then C1 = C1'-> Split The Name To Get 2 Parts NewName= Split(strFiles.Name,".")'-> Replace The First NewName V1 = Replace(NewName(0),"bobo" & C1)'-> Copy The File To It New Name objFSO.CopyFile strFiles , strNewDest & "\" & V1 & ".log", True End If End If Next Edited March 19, 2008 by gunsmokingman
kabucek Posted March 19, 2008 Author Posted March 19, 2008 line: 8error: variable is undefined: 'strFiles'do i need to do change the strFiles ?thanks
Yzöwl Posted March 19, 2008 Posted March 19, 2008 If you wanted to try a batch file try this:NewLog.cmd@Echo off&SetlocalSet "Source=L:"&Set "Destination=Q:"&Set "NewName=bobo"&Set "Extension=.log"Pushd %Source%For /f "delims=" %%# In ('dir/b/od/a-d *%Extension%') Do Set "OldName=%%#"Echo:F|Xcopy %OldName% %Destination%\%NewName%%Extension% /d>Nul
gunsmokingman Posted March 19, 2008 Posted March 19, 2008 Change thisDim C1, N, NewName, objFolder, objFSO, strFolder, strNewDest, V1Dim C1, N, NewName, objFolder, objFSO, strFiles, strFolder, strNewDest, V1I forgot to add that to the dimWhen using Option Explicit all varibles have to be dimSorry about the errors I am doing this from guessing.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now