Modify Existing Script with Array for Variable?

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 11 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
bjblackmore
Posts: 2
Last visit: Sun Apr 01, 2012 8:44 pm

Modify Existing Script with Array for Variable?

Post by bjblackmore »

Hi,
I have the following script, which I use to delete all subfolders from a folder, except the most recent (due to an application that fills the disk, and doesn't delete it's log files itself). I've now found that I need to delete a second set of subfolders from another folder, and I want to include the function in a single script.
I could just duplicate the code, specifying the second folder as 'sfolderpath2' but this seems to be a messy a long winded way of doing it. Can someone help me to modify the script, specifying both folder locations as an array or something, and then maybe using 'for each' or something so the code doesn't become to long, and messy!
Many thanks
Ben
----------------------------------------------Const adVarChar = 200Const adDate = 7
ipreserve=1 '# of folders to preservesfolderpath="c:templogs" 'your path to the folder
set fso=createobject("scripting.filesystemobject")if not fso.folderexists(sfolderpath) then set fso=nothing wscript.echo "The folder does not exist." & vbcrlf & sfolderpath & vbcrlf & "Operation aborted." wscript.quitend if
set ofolder=fso.getfolder(sfolderpath)set rs = createobject("ador.recordset")with rs.fields .append "subfolderpath",adVarChar,255 .append "datelastmodified",adDateend with
with rs .open for each osf in ofolder.subfolders .addnew array("subfolderpath","datelastmodified"), array(osf.path,osf.datelastmodified) .update nextend with
icount=0if not (rs.eof and rs.bof) then rs.sort="datelastmodified desc" rs.movefirst do while not rs.eof icount=icount+1 if icount>ipreserve then on error resume next fso.deletefolder rs.fields("subfolderpath"),true on error goto 0 end if rs.movenext loopend if
set rs=nothingset ofolder=nothingset fso=nothing
User avatar
bjblackmore
Posts: 2
Last visit: Sun Apr 01, 2012 8:44 pm

Modify Existing Script with Array for Variable?

Post by bjblackmore »

Hi,
I have the following script, which I use to delete all subfolders from a folder, except the most recent (due to an application that fills the disk, and doesn't delete it's log files itself). I've now found that I need to delete a second set of subfolders from another folder, and I want to include the function in a single script.
I could just duplicate the code, specifying the second folder as 'sfolderpath2' but this seems to be a messy a long winded way of doing it. Can someone help me to modify the script, specifying both folder locations as an array or something, and then maybe using 'for each' or something so the code doesn't become to long, and messy!
Many thanks
Ben
----------------------------------------------Const adVarChar = 200Const adDate = 7
ipreserve=1 '# of folders to preservesfolderpath="c:templogs" 'your path to the folder
set fso=createobject("scripting.filesystemobject")if not fso.folderexists(sfolderpath) then set fso=nothing wscript.echo "The folder does not exist." & vbcrlf & sfolderpath & vbcrlf & "Operation aborted." wscript.quitend if
set ofolder=fso.getfolder(sfolderpath)set rs = createobject("ador.recordset")with rs.fields .append "subfolderpath",adVarChar,255 .append "datelastmodified",adDateend with
with rs .open for each osf in ofolder.subfolders .addnew array("subfolderpath","datelastmodified"), array(osf.path,osf.datelastmodified) .update nextend with
icount=0if not (rs.eof and rs.bof) then rs.sort="datelastmodified desc" rs.movefirst do while not rs.eof icount=icount+1 if icount>ipreserve then on error resume next fso.deletefolder rs.fields("subfolderpath"),true on error goto 0 end if rs.movenext loopend if
set rs=nothingset ofolder=nothingset fso=nothing
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Modify Existing Script with Array for Variable?

Post by jvierra »

Make a function out of the code and call it twice with each folder path.

call it

deletesomething "c:somepath"
deletesomething "c:someother path"

function deletesomething( sfolderpath )
' put code in here
end fuction
This topic is 11 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked