Jump to content

Recommended Posts

Posted

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=Now

Set 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 If

Next

thanks


Posted

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.

Posted

I want to search for latest file with .log extension and then copy that file to new destination with new filename

i 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 ?

thanks

Set 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 If

Next

Sub CreateReportFile

'Creates or overwrites the report file in the directory of this script

Set 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 Sub

WScript.Echo NewestFile

Posted (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
Next

This uses the split and replace methods. The Split makes a 2 element array

Then you replace the first array element with the name you want.

Then you copy the new name plus "." and the second array element.

Edited by gunsmokingman
Posted

i've tried 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

V1 = Replace(NewName(0),"bobo")

'-> Copy The File To It New Name

objFSO.CopyFile strFiles , strNewDest & "\" & V1 & "." NewName(1), True

End If

End If

Next

then i got this :

Line: 15

char: 62

Expected end of statement

not sure what next..

thanks

Posted (edited)

Change this

'-> Copy The File To It New Name

objFSO.CopyFile strFiles , strNewDest & "\" & V1 & "." NewName(1), True

To this

'-> Copy The File To It New Name

objFSO.CopyFile strFiles , strNewDest & "\" & V1 & "." & NewName(1), True

When 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 Name

objFSO.CopyFile strFiles , strNewDest & "\" & V1 & ".log", True

Edit

I 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 by gunsmokingman
Posted

If you wanted to try a batch file try this:

NewLog.cmd

@Echo off&Setlocal
Set "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

Posted

Change this

Dim C1, N, NewName, objFolder, objFSO, strFolder, strNewDest, V1

Dim C1, N, NewName, objFolder, objFSO, strFiles, strFolder, strNewDest, V1

I forgot to add that to the dim

When using Option Explicit all varibles have to be dim

Sorry about the errors I am doing this from guessing.

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...