?
Solved

Rename File from FTP

Posted on 2010-11-19
6
Medium Priority
?
996 Views
Last Modified: 2012-05-10
I'm trying to create a vbs that will pull files from ftp and rename one of them. I have the ftp part working great but I can't figure out how to rename the file I need

The file needs to go from this ExportTemplate_0_20101119000037.txt to this LAST_EXPORT.txt
 Here is a list of files that get downloaded

ExportTemplate_0_20101117000024.txt
ExportTemplate_0_20101118000052.txt
ExportTemplate_0_20101119000037.txt

So I was think maybe of triming off the last six caracters so then it would be ExportTemplate_0_20101119.txt then just rename the file with todays date in the file name.


strScript = "runftp.txt"
strSite = "ftp.site.com"
strUsername = "Username"
strPassword = "Password" 
strFileName = "*.txt"
strDirectory = "c:\RTA_FILES"
strPrefix = "ExportTemplate_0"
strComputer = "."
strDayName = WeekDayName(Weekday(Date))
strLastFileName = strPrefix & "_" & YYYYMMDD(Date) & "0000" & ".txt"

strFullPath = strDirectory & "\" & strLastFileName
MsgBox strFullPath
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell: Set objShell=CreateObject("Wscript.Shell")

If objFSO.FolderExists(strDirectory) Then
Else
   objFSO.CreateFolder(strDirectory)
End If


Set objScript = objFSO.CreateTextFile(strDirectory & "\" & strScript, True)
objScript.WriteLine "open " & strSite
objScript.WriteLine strUsername
objScript.WriteLine strPassword
objScript.WriteLine "lcd " & strDirectory
objScript.WriteLine "mget " & Chr(34) & strFileName & Chr(34)
objScript.WriteLine "bye "
objScript.Close
 
Set objShell = CreateObject("WScript.Shell")
objShell.Run "ftp -i -s:" & Chr(34) & strScript & Chr(34) 
objFSO.DeleteFile(strDirectory & "\" & strScript), True

 Function YYYYMMDD(dtmTime)
    strMonth = Right("0" & Month(dtmTime), 2)
    strDay = Right("0" & Day(dtmTime), 2)
    strYear = Right(Year(dtmTime), 4)
    
    YYYYMMDD = strYear & strMonth & strDay
End Function

Open in new window

0
Comment
Question by:JHopkins213
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 6

Expert Comment

by:stavros41
ID: 34173359
Try this:
Dim objFSO

Set objFSO = CreateObject("Scripting.FileSystemObject")

strDirectory = "C:\TEMP"

Set objFolder = objFSO.GetFolder(strDirectory & "\")
For Each item In objFolder.Files
  If InStr(item.Name, "ExportTemplate") <> 0 Then
  	 strNewName = "LAST_EXPORT.txt"
	 item.Name = strNewName	 
  End If    
Next

Open in new window

0
 

Author Comment

by:JHopkins213
ID: 34173445
Gives and error about file already exists

I have multiple files I need to rename the last one

Example

C:\RTA_FILES\ExportTemplate_0_20101116000004.txt
C:\RTA_FILES\ExportTemplate_0_20101117000024.txt
C:\RTA_FILES\ExportTemplate_0_20101118000052.txt
C:\RTA_FILES\ExportTemplate_0_20101119000037.txt <-- This is the newest file in the directory and it needs to be renamed to LAST_EXPORT.txt
0
 
LVL 6

Accepted Solution

by:
stavros41 earned 2000 total points
ID: 34173495
Is it always the same file name? If so change this line:
If InStr(item.Name, "ExportTemplate_0_20101119000037") <> 0 Then

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 57

Expert Comment

by:Bill Prew
ID: 34173575
How about this?

strScript = "runftp.txt"
strSite = "ftp.site.com"
strUsername = "Username"
strPassword = "Password" 
strFileName = "*.txt"
strDirectory = "c:\RTA_FILES"
strPrefix = "ExportTemplate_0"
strComputer = "."
strDayName = WeekDayName(Weekday(Date))
strLastFileName = strPrefix & "_" & YYYYMMDD(Date) & "*" & ".txt"
strFullPath = strDirectory & "\" & strLastFileName
MsgBox strFullPath

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell: Set objShell=CreateObject("Wscript.Shell")

If Not objFSO.FolderExists(strDirectory) Then
   objFSO.CreateFolder(strDirectory)
End If

Set objScript = objFSO.CreateTextFile(strDirectory & "\" & strScript, True)
objScript.WriteLine "open " & strSite
objScript.WriteLine strUsername
objScript.WriteLine strPassword
objScript.WriteLine "lcd " & strDirectory
objScript.WriteLine "mget " & Chr(34) & strFileName & Chr(34)
objScript.WriteLine "bye "
objScript.Close
 
Set objShell = CreateObject("WScript.Shell")
objShell.Run "ftp -i -s:" & Chr(34) & strScript & Chr(34) 
objFSO.DeleteFile(strDirectory & "\" & strScript), True

objFSO.CopyFile strFullPath, strDirectory & "\Last_Export.txt"

Function YYYYMMDD(dtmTime)
   strMonth = Right("0" & Month(dtmTime), 2)
   strDay = Right("0" & Day(dtmTime), 2)
   strYear = Right(Year(dtmTime), 4)
   YYYYMMDD = strYear & strMonth & strDay
End Function

Open in new window


~bp
0
 

Author Comment

by:JHopkins213
ID: 34173709
Here is what I ended up doing

stavros41's code was exactly what I needed
strScript = "runftp.txt"
strSite = "ftp.site.com"
strUsername = "password"
strPassword = "GBRTA2sys" 
strFileName = "*.txt"
strDirectory = "c:\RTA_FILES"
strPrefix = "ExportTemplate_0"
strLastFileName = strPrefix & "_" & YYYYMMDD(Date) & "0000" 

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell: Set objShell=CreateObject("Wscript.Shell")

If objFSO.FolderExists(strDirectory) Then
	objFSO.DeleteFolder(strDirectory)
	objFSO.CreateFolder(strDirectory)
Else
   objFSO.CreateFolder(strDirectory)
End If


Set objScript = objFSO.CreateTextFile(strDirectory & "\" & strScript, True)
objScript.WriteLine "open " & strSite
objScript.WriteLine strUsername
objScript.WriteLine strPassword
objScript.WriteLine "lcd " & strDirectory
objScript.WriteLine "mget " & Chr(34) & strFileName & Chr(34)
objScript.WriteLine "bye "
objScript.Close
 
Set objShell = CreateObject("WScript.Shell")
strCommand = "cmd /c ftp -i -s:" & Chr(34) & strScript & Chr(34) 
objShell.Run strCommand, 0, True
objFSO.DeleteFile(strDirectory & "\" & strScript), True


Set objFolder = objFSO.GetFolder(strDirectory & "\")
For Each item In objFolder.Files
  If InStr(item.Name, strLastFileName) <> 0 Then 
  	 strNewName = "LAST_EXPORT.txt"
	 item.Name = strNewName	 
  End If    
Next 


 Function YYYYMMDD(dtmTime)
    strMonth = Right("0" & Month(dtmTime), 2)
    strDay = Right("0" & Day(dtmTime), 2)
    strYear = Right(Year(dtmTime), 4)
    
    YYYYMMDD = strYear & strMonth & strDay
End Function

Open in new window

0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 34173743
==> Here is what I ended up doing

Hmm, just be careful of that, I don't think there is any guarantee that the For Each loop returns files sorted by name.  I suspect it just pulls them by the order they are in the directory, which could be random.

~bp
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question