Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Move one file based on date/time but leave others. 2

Status
Not open for further replies.

craigey

Technical User
Apr 18, 2002
510
GB
Hi,

I have a vbscript that backs up a couple of mysql databases every 6 hours to a backup directory on the D:\ drive. The filenames are formated with the date, time & server name, like so:

servername_Sat-04-06-2011_00_30_02.zip
servername_Sat-04-06-2011_06_30_02.zip
servername_Sat-04-06-2011_12_30_02.zip
servername_Sat-04-06-2011_18_30_02.zip
servername_Fri-03-06-2011_00_30_02.zip
servername_Fri-03-06-2011_06_30_02.zip
servername_Fri-03-06-2011_12_30_02.zip
servername_Fri-03-06-2011_18_30_02.zip
servername_Thu-02-06-2011_00_30_02.zip
servername_Thu-02-06-2011_06_30_02.zip
servername_Thu-02-06-2011_12_30_02.zip
servername_Thu-02-06-2011_18_30_02.zip

I would like to upload one file of each day to FTP. I can do the upload & the FTP etc, but have no idea how to select only one file of each day to upload.

Can anyone point me in the right direction?

Thanks,

Craig
 
Parse out the date info and use a dictionary object to keep track of the dates. If the date does not yet exist in the dictionary, add it. At the end of the script the dictionary will only contain 1 reference to each date.

Code:
set odic=createobject("scripting.dictionary")
dim fileName(11)
'fill an array with some sample data
fileName(0) = "servername_Sat-04-06-2011_00_30_02.zip"
fileName(1) = "servername_Sat-04-06-2011_06_30_02.zip"
fileName(2) = "servername_Sat-04-06-2011_12_30_02.zip"
fileName(3) = "servername_Sat-04-06-2011_18_30_02.zip"
fileName(4) = "servername_Fri-03-06-2011_00_30_02.zip"
fileName(5) = "servername_Fri-03-06-2011_06_30_02.zip"
fileName(6) = "servername_Fri-03-06-2011_12_30_02.zip"
fileName(7) = "servername_Fri-03-06-2011_18_30_02.zip"
fileName(8) = "servername_Thu-02-06-2011_00_30_02.zip"
fileName(9) = "servername_Thu-02-06-2011_06_30_02.zip"
fileName(10) = "servername_Thu-02-06-2011_12_30_02.zip"
fileName(11) = "servername_Thu-02-06-2011_18_30_02.zip"

'here is the meat of the example
for i = 0 to ubound(fileName)

  varTemp = right(fileName(i), 23)
  varTemp = left(varTemp, 10)
  'wscript.echo(varTemp)
  if not odic.exists(varTemp) then
    odic.add varTemp, fileName(i)
  end if
next

if odic.count<>0 then
  for each key in odic.keys
    wscript.echo odic(key)
  next
end if

This example disregards the timestamp and servername information. If your needs are more specific, such as finding the latest timestamp for a given day or uploading 1 file per day per server, more logic will have to be added.
 
If the files will always be named as posted and an FTP upload will occur once every week, then jges suggestion is simplest - no need for timestamps and such.

However, I would automate file discovery instead of manually building an array.

Code:
set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
set objDic = WScript.CreateObject("Scripting.Dictionary")

set objFiles = objFSO.GetFolder("\\server\path_to_files").Files
for each objFile in objFiles
    intPos = inStr(objFile.Name, "_")
    strDay = mid(objFile.Name, intPos, 3)
    if NOT (objDic.exists(strDay)) then
        objDic.add strDay, objFile.Name
    end if
next

if (objDic.count <> 0) then
    for each strKey in objDic.keys
        msgbox objDic(strKey)
    next 
end if

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
However, I would automate file discovery instead of manually building an array.
Agreed, the array I used was for testing purposes and should not be used in the actual script. My intent was to illustrate the parsing of the date info and use of the dictionary object.
 
Thank you both.
jges your example was perfect & would have been sufficent on it's own for me to get this done. Also Geates thanks for saving me some time in modifying the script to automatically populate the array.

I've never used the dictionary object before, but I will do some further reading up on it as it is obviously very useful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top