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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tickler system to move files

Status
Not open for further replies.

cantina

MIS
Aug 30, 2004
14
US
Hi,

I'm new to vbscript, and was wondering how best to tackle this situation:

I have 2 directories called archive1 and archive2.
I have a directory called backups
Within backups, I have 4 separate subdirectories. Within these 4 subdirs, I have a bunch of backup files. I would like to move all the backup files to the archive folder after 1 day. The next day, I'd like to move (not copy) the contents of archive1 to archive 2. So in essence, this would be a system that moves the backup files and keeps them for 2 days, at which time, they are overwritten after 2 days by the next set, and so on. What I'm struggling with is how to write the script that drills down deep into each of the subdirectories under backups, looks for the backup files, and moves them to the archive, while keeping the entire original backup directory structure intact.

Thanks for any ideas and suggestions.
T
 
Look at the FileSystem object. The .DeleteFolder method will delete the contents of the Archive2 folder. Then .MoveFolder method can be used to move the contents of Archive1 to Archive2. The .MoveFolder or .CopyFolder method can be used to get the contents of Backup into Archive1 depending on whether you want to leave files in Backup or not.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
MoveFolder will end up with the folders under Archive1/2.

To move just all files ? you need to loop through the subfolders:

set FSO = CreateObject( "Scripting.FileSystemObject")
set rootDir = FSO.GetFolder( "c:\____") '(<- enter path)
For Each subDir in folder1.SubFolders
FSO.MoveFiles subDir.Path +"\*.*", "c:\____\Archive1\"
Next
 
Hi, here is what i have:

on error resume next
dim strDIR, dstDir
Set FSO = WScript.CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
strDir = "c:\backup"
dstDir = "C:\archive"
Set objDir = FSO.GetFolder(strDir)
getInfo(objDir)
Function getInfo(pCurrentDir)
For Each aItem In pCurrentDir.Files
'unmodify the following if the clients have just been sending PGP files and you trust them...
wscript.Echo aItem.Name
If LCase(Right(Cstr(aItem.Name), 4)) = ".bkf" Then
Msgbox aItem.Path
fso.movefile aItem,dstDir
End If
Next
For Each aItem In pCurrentDir.SubFolders
wscript.Echo aItem.Name & " passing recursively"
getInfo(aItem)
Next
End Function

When I run this, though, nothing really happens. If I take the "on error resume next" off of the script, the script errors out and states: File already exists 800a003a
Can anyone spot what's wrong?

Thanks...
 
Hello cantina,

There are quite a few problems.
[1] You seem to want to movefile to a flat structured destiny. Every file of .bkf within subfolders are moved to a single folder dstDir without subfolder structure. Is that what you want? (I take it as yes.)
[2] In .movefile you pass a string (the path) as the first argument, not the file _object_ itself.
[3] Since no wildcard is involved, the destination must be the full path name, not just the folder name.
[4] You have to check if the same named file exists at the destination. If yes, you have to decide whether to move. If you decide to move, then you have to delete the existing one at the destination before the move.
[5] Since you do not have any return value, make it a sub rather than function, although this is not 100% necessary.
Code:
dim strDIR, dstDir
Set FSO = WScript.CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
strDir = "c:\backup"
dstDir = "C:\archive"

if not fso.folderexists(strDir) then wscript.quit(1)
if not fso.folderexists(dstDir) then
    fso.createfolder dstDir
end if

Set objDir = FSO.GetFolder(strDir)
getInfo objDir

Sub getInfo(pCurrentDir)
For Each aItem In pCurrentDir.Files
'unmodify the following if the clients have just been sending PGP files and you trust them...
    wscript.Echo aItem.Name
    If LCase(Right(Cstr(aItem.Name), 4)) = ".bfk" Then
        Msgbox aItem.Path
	if fso.fileexists(dstDir & "\" & aItem.name) then
		fso.deletefile dstDir & "\" & aItem.name
	end if
    fso.movefile aItem.path,dstDir & "\" & aItem.name
    End If
Next
For Each aItem In pCurrentDir.SubFolders
    'wscript.Echo aItem.Name & " passing recursively"
    getInfo aItem
Next 
End Sub
regards - tsuji
 
Thanks tsuji...your fixes and pointers were most helpful. Just for my fyi and enrichment, if i wanted to include the subfolder structure and the *.bkf files within these structures in the move, what tweaks would need to be made?

Thanks,
Cantina
 
cantina,

To mimic original subfolder structure, you have to create along the way. These are a step you have to take. [1] Get the path of the source (as above). [2] Split it into array with separator "\". Then the first (0th) is the drive which is of no interest. The last is the file.name neither. The middle elements are path element. From lower to higher you rebuild the path and check the existfolder() at the destination drive and base folder. If not exist, create it along the way. [3] Then you do the move file.

- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top