MSFN Forum: change name while copying a file in vbs? - MSFN Forum

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

change name while copying a file in vbs? change name of destination file Rate Topic: -----

#1 User is offline   kabucek 

  • Member
  • PipPip
  • Group: Members
  • Posts: 102
  • Joined: 07-August 06

Posted 17 March 2008 - 07:44 AM

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


#2 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,118
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 17 March 2008 - 08:47 AM

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.

#3 User is offline   kabucek 

  • Member
  • PipPip
  • Group: Members
  • Posts: 102
  • Joined: 07-August 06

Posted 17 March 2008 - 08:51 AM

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

#4 User is offline   gunsmokingman 

  • MSFN Addict
  • Group: Super Moderator
  • Posts: 1,991
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 17 March 2008 - 08:55 AM

Try this

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 User is offline   kabucek 

  • Member
  • PipPip
  • Group: Members
  • Posts: 102
  • Joined: 07-August 06

Posted 19 March 2008 - 08:41 AM

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

#6 User is offline   gunsmokingman 

  • MSFN Addict
  • Group: Super Moderator
  • Posts: 1,991
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 19 March 2008 - 09:53 AM

Change this

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 User is offline   kabucek 

  • Member
  • PipPip
  • Group: Members
  • Posts: 102
  • Joined: 07-August 06

Posted 19 March 2008 - 01:22 PM

line: 8

error: variable is undefined: 'strFiles'


do i need to do change the strFiles ?

thanks

#8 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,118
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 19 March 2008 - 03:14 PM

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


#9 User is offline   gunsmokingman 

  • MSFN Addict
  • Group: Super Moderator
  • Posts: 1,991
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 19 March 2008 - 03:30 PM

Change this

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.

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy