Windows 7 - vbscript - move files older than 180days based on modified datekeeping the original directory structure

Asked By Kouliscon on 16-Mar-11 09:17 AM
Hello All
I found  a vbscript on the net and i need help to modified in order to
meet my needs since i dont know how to do it.
the script is:
-----------------------------------BEGIN OF
SCRIPT---------------------------------------------------

Dim objFSO, ofolder, objStream
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("scripting.filesystemobject")
Set objNet = CreateObject("WScript.NetWork")
Set FSO = CreateObject("Scripting.FileSystemObject")
set outfile = fso.createtextfile("Move-Result.txt",true)
SPath = "c:\temp\"
ShowSubfolders FSO.GetFolder(spath)
Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
CheckFolder(subfolder)
ShowSubFolders Subfolder
Next
End Sub
'CheckFolder(objFSO.getFolder(SPath))
Sub CheckFolder(objCurrentFolder)
Dim strTempL, strTempR, strSearchL, strSearchR, objNewFolder, objFile
Const OverwriteExisting = TRUE
currDate = Date
dtmDate = Date - 180
strTargetDate = ConvDate(dtmDate)
For Each objFile In objCurrentFolder.Files
FileName = objFile
'WScript.Echo FileName
strDate = ConvDate(objFile.DateCreated)
'strDate = ConvDate(objFile.DateLastModified)
If strDate < strTargetDate Then
objFSO.MoveFile FileName, "e:\test\"
outfile.writeline filename
End If
Next
End Sub
Function ConvDate (sDate) 'Converts MM/DD/YYYY HH:MM:SS to string
YYYYMMDD
strModifyDay = day(sDate)
If len(strModifyDay) < 2 Then
strModifyDay = "0" & strModifyDay
End If
strModifyMonth = Month(sDate)
If len(strModifyMonth) < 2 Then
strModifyMonth = "0" & strModifyMonth
End If
strModifyYear = Year(sDate)
ConvDate = strModifyYear & strModifyMonth & strModifyDay
End Function
---------------------------------------------END OF
SCRIPT-------------------------------------------

the thing is that i want help to the following:

1) to check the files in the folders and subfolders of the location c:
\temp and based on the MODIFIED DATE to move them to the location e:
\test

2) To keep the same exact directory structure in the new location e:
\test

3) to have the ability to place somewere *.* to select all files from
c:\temp or to put *.xls and to find and move only the .xls files that
havents been used for 180 days with the same directory structure to e:
\test.

Since i want to deploy this script with GPO i would like to replace
the e:\test to an external HDD with the variable of the username. I
intent to do this per user.

Any help will be appreciated.

thx




Mayayana replied to Kouliscon on 16-Mar-11 10:49 AM
You can download the help file here:
http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207D-4BE1-8A76-1C4099D7BBB9

Pretty much everything you want to do is included
in the methods of the FileSystemObject object.

---------------------------------------
| Hello All
| I found  a vbscript on the net and i need help to modified in order to
| meet my needs since i dont know how to do it.
| the script is:
| -----------------------------------BEGIN OF
| SCRIPT---------------------------------------------------
|
| Dim objFSO, ofolder, objStream
| Set objShell = CreateObject("WScript.Shell")
| Set objFSO = CreateObject("scripting.filesystemobject")
| Set objNet = CreateObject("WScript.NetWork")
| Set FSO = CreateObject("Scripting.FileSystemObject")
| set outfile = fso.createtextfile("Move-Result.txt",true)
| SPath = "c:\temp\"
| ShowSubfolders FSO.GetFolder(spath)
| Sub ShowSubFolders(Folder)
| For Each Subfolder in Folder.SubFolders
| CheckFolder(subfolder)
| ShowSubFolders Subfolder
| Next
| End Sub
| 'CheckFolder(objFSO.getFolder(SPath))
| Sub CheckFolder(objCurrentFolder)
| Dim strTempL, strTempR, strSearchL, strSearchR, objNewFolder, objFile
| Const OverwriteExisting = TRUE
| currDate = Date
| dtmDate = Date - 180
| strTargetDate = ConvDate(dtmDate)
| For Each objFile In objCurrentFolder.Files
| FileName = objFile
| 'WScript.Echo FileName
| strDate = ConvDate(objFile.DateCreated)
| 'strDate = ConvDate(objFile.DateLastModified)
| If strDate < strTargetDate Then
| objFSO.MoveFile FileName, "e:\test\"
| outfile.writeline filename
| End If
| Next
| End Sub
| Function ConvDate (sDate) 'Converts MM/DD/YYYY HH:MM:SS to string
| YYYYMMDD
| strModifyDay = day(sDate)
| If len(strModifyDay) < 2 Then
| strModifyDay = "0" & strModifyDay
| End If
| strModifyMonth = Month(sDate)
| If len(strModifyMonth) < 2 Then
| strModifyMonth = "0" & strModifyMonth
| End If
| strModifyYear = Year(sDate)
| ConvDate = strModifyYear & strModifyMonth & strModifyDay
| End Function
| ---------------------------------------------END OF
| SCRIPT-------------------------------------------
|
| the thing is that i want help to the following:
|
| 1) to check the files in the folders and subfolders of the location c:
| \temp and based on the MODIFIED DATE to move them to the location e:
| \test
|
| 2) To keep the same exact directory structure in the new location e:
| \test
|
| 3) to have the ability to place somewere *.* to select all files from
| c:\temp or to put *.xls and to find and move only the .xls files that
| havents been used for 180 days with the same directory structure to e:
| \test.
|
| Since i want to deploy this script with GPO i would like to replace
| the e:\test to an external HDD with the variable of the username. I
| intent to do this per user.
|
| Any help will be appreciated.
|
| thx
Kouliscon replied to Mayayana on 16-Mar-11 10:00 AM
tails.aspx?FamilyId=3D01592C48-207...
o

Thx for your answer, i need help to modify the script by someone that
know... anybody?
Mayayana replied to Kouliscon on 16-Mar-11 12:26 PM
Thx for your answer, i need help to modify the script by someone that
know... anybody?

You're not asking for help. You're asking for someone
to write it for you so that you do not have to learn how
to do it yourself. Generally it is customary to offer
payment when you ask someone to do your work for
you.
Kouliscon replied to Mayayana on 17-Mar-11 02:49 AM
Hello

I am only asking for help or a direction....

Thx anyways
Dave Lawrence replied to Kouliscon on 19-Mar-11 09:17 AM
at a high level, i think the simple way to proceed is to make a list of all your target files (i.e. xls files) in your source directory (c:\temp)



you "could" follow the method shown in your sample code (i.e. using recursion), but this seems pretty inefficient since windows already offers a way to recurse sub-directories when looking for files. (i.e. dir /s /b *.xls). so ...



1. if you pipe the result of this into a file, then you just have to traverse the file list to test your date requirements (i.e. older then 180 days).



2. once you have this pared down list of files that must be moved, you can use this to identify the required directory tree you'll need at e:\test



3. create that tree structure



4. use your list to copy the relevant files from your source (c:\temp) to your target (e:\test).
Dave Lawrence replied to Dave Lawrence on 19-Mar-11 02:54 PM
Kouliscon,



For the record, i happen to agree with the comments made by Mayayana, but ... that said, i have gleaned SO much help from online forums such as this one, that i have taken this as a chance to 'payback' some of what I've taken. This script is 'as is'. there is always chance on your system that it will have glitches or otherwise encounter a run time error (this was run on windows 7 professional 64 bit ..?). It "seems" to do the task you described.



and BTW, you're probably going to have to resolve any line feed/carriage return issues when you copy and paste this into a local script ... i always use 80 column formatting ...

----------------- Begin Script -------------------



'*********************************************************

' Instructions:

'   Set srcfold  = YOUR source folder

'   Set srcext   = The kind of files you want (.xls, .txt ?)

'   Set trgtfold = where you want the files to move to

'   Set MAX_FILE_AGE = date threshold to move them

'*********************************************************





Set FSO = CreateObject("Scripting.FileSystemObject")



Const ForReading = 1, ForWriting = 2, ForAppending = 8

Const MAX_FILE_AGE = 3 ' days





srcfold = FSO.GetSpecialFolder(2)

srcext = "txt"

'wscript.echo srcfold & vbcrlf

trgtfold = "e:\temp" ' set this to



if not fso.FolderExists(trgtfold) then

fso.CreateFolder(trgtfold)

end if



' break this list / string of filenames down into individual file names

fileset = split(GetFileList,vbcrlf)



' process each file in turn ...

for each itmFileName in fileset

if fso.FileExists(itmFileName) then

Set tFile =  fso.getfile(itmFileName)

file_age = cint(date - tFile.DateCreated)

if file_age > MAX_FILE_AGE then

trgtFileloc = trgtfold & replace(tFile.ParentFolder, srcfold,"")

if not fso.FolderExists(trgtFileloc) then

if not MakeDir(trgtFileloc) then

wscript.echo "unable to create path: " & trgtFileloc

end if

end if

'wscript.echo trgtFileloc & "\" & tFile.Name

tFile.Move trgtFileloc & "\" & tFile.Name, True

end if

end if

next

'*******************************************

' Make a temp file with list of files

' matching enumerated file ext - srcext

'*******************************************

Function GetFileList

Dim oShell

Set oShell = CreateObject("Wscript.Shell")



' Generate a temporary file for listing all matching files

TempFilename = FSO.buildpath(FSO.GetSpecialFolder(2), FSO.GetTempName)



cmdstr = "cmd.exe /c dir /b/s " & srcfold & "\*." & srcext & " >" & TempFilename

'wscript.echo cmdstr & vbcrlf

oShell.Run cmdstr, 0, true



' wait up to 10 seconds for file to be created

cnt = 0

while cnt < 10 and not FSO.FileExists(TempFilename)

wscript.sleep(1000)

cnt = cnt + 1

wend



' retrieve file list and delete temp file...

if FSO.FileExists(TempFilename) then

if FSO.getFile(TempFilename).size > 0 then

Set f = fso.OpenTextFile(TempFilename, ForReading, True)

GetFileList  = f.readall

f.Close

else

GetFileList = ""

wscript.echo "Generate List of files failed!"

end if



FSO.deletefile TempFilename, true

else

GetFileList = ""

wscript.echo "Generate List of files failed!"

end if



End Function



'*******************************************

' Create the specified path

' i.e. creates all non-existent

' intermediate and target folders

'*******************************************

Function MakeDir (strPath)

Dim strParentPath

On Error Resume Next

strParentPath = FSO.GetParentFolderName(strPath)



If Not FSO.FolderExists(strParentPath) Then MakeDir strParentPath

If Not FSO.FolderExists(strPath) Then FSO.CreateFolder strPath

On Error Goto 0

MakeDir = FSO.FolderExists(strPath)

End Function



----------------- end Script -------------------



good luck with this and enjoy. I only ask that you reply if you use it with any fix's you make (i am curious to see if and/or where it has 'difficulties').



Dave
Dr J R Stockton replied to Kouliscon on 17-Mar-11 04:40 PM
In microsoft.public.scripting.vbscript message <042c0b05-65f7-4095-970b-


Function ConvDate (sDate) ' CDate to string YYYYMMDD
ConvDate = CStr(Year(sDate)*1e4 + Month(sDate)*1e2 + day(sDate))
End Function

Commonly, the context will make the CStr() unnecessary.

Your comment is wrong, because the argument is stored as a float of days
from 1899-12-30 local = 0 rather than in FFF.

--
(c) John Stockton, nr London, UK.    ?@merlyn.demon.co.uk     Turnpike v6.05.
Website  <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.
Al Dunbar replied to Kouliscon on 17-Mar-11 07:53 PM
The problem is, nobody here reads minds well enough to know where your
difficulty lies. It is foolhardy to try to modify a script without having at
least some basic understanding. If that is what you want help with, try
asking about the part of that script that you do not understand.

/Al