change name while copying a file in vbs? change name of destination file
#1
Posted 17 March 2008 - 07:44 AM
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
#2
Posted 17 March 2008 - 08:47 AM
Could you please tell us specifically what it is you want to do and perhaps a more appropriate script could be provided.
#3
Posted 17 March 2008 - 08:51 AM
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
#4
Posted 17 March 2008 - 08:55 AM
Quote
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.
This post has been edited by gunsmokingman: 17 March 2008 - 09:08 AM
#5
Posted 19 March 2008 - 08:41 AM
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
#6
Posted 19 March 2008 - 09:53 AM
Quote
'-> Copy The File To It New Name
objFSO.CopyFile strFiles , strNewDest & "\" & V1 & "." NewName(1), True
To this
Quote
'-> 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
Quote
'-> 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.
Quote
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
This post has been edited by gunsmokingman: 19 March 2008 - 10:12 AM
#7
Posted 19 March 2008 - 01:22 PM
error: variable is undefined: 'strFiles'
do i need to do change the strFiles ?
thanks
#8
Posted 19 March 2008 - 03:14 PM
@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
#9
Posted 19 March 2008 - 03:30 PM
Quote
Dim C1, N, NewName, objFolder, objFSO, strFolder, strNewDest, V1
Quote
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.
- ← Best way to learn Windows Programming?
- Programming (C++, Delphi, VB/VBS, CMD/batch, etc.)
- [Req] Script →



Help

Back to top









