This is a multi-part message in MIME format.
------=_NextPart_000_01A3_01C526AA.CB4F12E0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Hi all,
I've written this script to allow users to customize a view for a =
folder and then apply to all the subfolders. I thought I'd post it here =
first for comments and suggestions. It's not very robust with error =
handling :). It will hang if it runs into a folder with access denied. =
Can the gurus here take a look and help me tweak it before posting to =
the customize and music folk? I'm especially wondering about how to =
handle the timing in the OpenAndCloseSubfolders function. The loop I =
have now seems to be faster than using a sleep statement. I've left in =
a msgbox reporting execution time for the function which I plan to take =
out before the big rollout :)
Thanks to all ,
Keith
P.S. I know some people hate it, but I'm attaching the file as text as =
well because of line-wrapping concerns.
##########################################################
Const conBagMRUPath =3D =
"HKEY_CURRENT_USER\Software\Microsoft\Windows\ShellNoRoam\BagMRU"
Const conBagsPath =3D =
"HKEY_CURRENT_USER\Software\Microsoft\Windows\ShellNoRoam\Bags"
Const conFolderStreams =3D =
"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Streams\Defaults=
\{F3364BA0-65B9-11CE-A9BA-00AA0=AD04AE837}"
Const ForReading =3D 1
dim oAppShell, oScriptShell, oFso, oFolder
dim VPathCollection ()
dim sIntroMsg, sScriptTitle, sIntro2, sSetView
'Instructions & Warnings
InitMsgStrs
If (msgbox(sIntroMsg, vbYesNo, sScriptTitle) <> vbYes) Then
WScript.Quit
End If
If (msgbox(sIntro2, vbYesNo + vbCritical, sScriptTitle) <> vbYes) Then
WScript.Quit
End If
'Assign objects
Set oAppShell =3D CreateObject("Shell.Application")
Set oScriptShell =3D CreateObject("WScript.Shell")=20
Set oFSO =3D CreateObject("Scripting.FileSystemObject")
'Verify other windows are closed
Do While oAppShell.windows.count <> 0
If (msgbox(oAppShell.windows.count & " Explorer/I.E. windows are =
still open!", _
vbRetryCancel + vbCritical, sScriptTitle) <> vbReTry) Then
WScript.Quit
End If
Loop
'Back up the Registry Keys
Call RegExport(conBagMRUPath, "BagMRU Backup.reg")
Call RegExport(conBagsPath, "Bags Backup.reg")
DeleteBagsAndIncreaseLimit
'If conFolderStreams exists,it screws up the
' default columns for detail view of special folders. So...
On Error Resume Next
oScriptShell.RegDelete conFolderStreams
Err.Clear
On Error Goto 0
iNewBagCount =3D 0
bContinue =3D True
iLoopCount =3D 0
Do While bContinue '*** The Fun Starts Here ***
iLoopCount =3D iLoopCount + 1
set oFolder =3D oAppShell.BrowseForFolder(0, "Choose a Folder", 0)
oAppShell.Open oFolder
WScript.Sleep 1000
msgbox sSetView,,oFolder.title & " - Script Paused..."
oAppShell.windows.item.quit
iNewBagCount =3D iNewBagCount + 1
ReDim Preserve VPathCollection(iLoopCount - 1)
VPathCollection(iLoopCount - 1) =3D VPath(oFolder)
sFolderTemplateFile =3D MakeFolderTemplate(iNewBagCount)
RegImport sFolderTemplateFile
sStart =3D Now
iNewBagCount =3D iNewBagCount + OpenAndCloseSubfolders(oFolder)
MsgBox "Finished in " & DateDiff("s", sStart, Now) & " seconds", _=20
vbSystemModal, "OpenAndCloseSubfolders"
oFso.GetFile(sFolderTemplateFile).Delete
oScriptShell.RegDelete(conBagsPath & "\AllFolders\Shell\")
oScriptShell.RegDelete(conBagsPath & "\AllFolders\")
msg =3D "You have saved views for the following folder(s):" & vbCrLf
For each sPath in VPathCollection
msg =3D msg & vbTab & sPath & vbCrLf
Next
msg =3D msg & "You have saved views for " & iNewBagCount & " folders =
(8000 max)" & vbCrLf
msg =3D msg & vbCrLf & "Would you like to do another folder?"
bcontinue =3D msgbox (msg, vbYesNo) =3D vbYes
Loop
Set oAppShell =3D Nothing
Set oScriptShell =3D Nothing
Set oFSO =3D Nothing
'---------------------------------------End------------------------------=
-----
Function MakeFolderTemplate (iBagNum)
Dim sTemplateRegPath, sTemplateFileName, sFolderTemplate, ts
sTemplateRegPath =3D conBagsPath & "\" & iBagNum
sTemplateFileName =3D "Folder Template" & iBagNum & ".reg"
sFolderTemplate =3D RegExport (sTemplateRegPath, sTemplateFileName)
'msgbox sFolderTemplate
Set ts =3D oFso.OpenTextFile(sFolderTemplate, ForReading)
sTemplateInfo =3D ts.ReadAll
ts.close
sSearch =3D "Bags\" & iNewBagCount
sReplace =3D "Bags\AllFolders"
sTemplateInfo =3D Replace (sTemplateInfo, sSearch, sReplace, 1, 2)
sTemplateInfo =3D Replace (sTemplateInfo, "My", "")
Set ts =3D oFso.CreateTextFile (sFolderTemplate, true)
ts.Write sTemplateInfo
ts.Close
Set ts =3D Nothing
MakeFolderTemplate =3D sFolderTemplate =20
End Function
'----------------------------------------
Function OpenAndCloseSubfolders (oFolder)
Dim iCount
iCount=3D0
For Each oItem in oFolder.Items
If oItem.IsFolder Then
If NotZipped(oItem) and NotAlreadySaved(oItem) Then
'Do Until oAppshell.windows.count =3D 0
' WScript.Sleep 100
'Loop
oAppshell.open oItem.GetFolder
'WScript.Sleep 1000
On Error Resume Next
Do
Err.Clear
oAppshell.windows.item.quit
Loop Until Err.Number =3D 0
iCount =3D iCount + 1
iCount =3D iCount + =
OpenAndCloseSubfolders(oItem.GetFolder)
End If
End If
Next
OpenAndCloseSubfolders =3D iCount
End Function
'-----------------------------------------
Sub DeleteBagsAndIncreaseLimit ()
Dim tFolder, tName, tFile, tString
Const TemporaryFolder =3D 2
Set tFolder =3D oFso.GetSpecialFolder(TemporaryFolder)
tName =3D oFso.GetTempName =20
Set tFile =3D tFolder.CreateTextFile(tName)
tString =3D _
"REGEDIT4" & vbCrLf & vbCrLf & _
"[-HKEY_CURRENT_USER\Software\Microsoft\Windows\ShellNoRoam\BagMRU]" =
& vbCrLf & _
"[-HKEY_CURRENT_USER\Software\Microsoft\Windows\ShellNoRoam\Bags]" & =
vbCrLf & vbCrLf & _
"[HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell]" & vbCrLf & _
"""BagMRU Size""=3Ddword:00001f40" & vbCrLf & vbCrLf & _
"[HKEY_CURRENT_USER\Software\Microsoft\Windows\ShellNoRoam]" & =
vbCrLf & _
"""BagMRU Size""=3Ddword:00001f40" & vbCrLf & vbCrLf & _
=
"[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Ex=
plorer]" & vbCrLf & _
"""NoSaveSettings""=3Ddword:00000000" & vbCrLf & vbCrLf
tFile.write (tString)
tFile.close
RegImport (tFolder.Path & "\" & tName)
oFso.GetFile(tFolder.Path & "\" & tName).Delete
End Sub =20
'-----------------------------------------
Sub RegImport (sFileName)
Dim sCmd
sCmd =3D "regedit.exe /s """ & sFileName & """"
oScriptShell.Run sCmd, 0, True
End Sub
'-----------------------------------------
Function RegExport (sRegPath, sFileName)
Dim sCmd, sTemp
sTemp =3D oScriptShell.SpecialFolders("Desktop") & "\" & sFileName
sCmd =3D "regedit.exe /A """ & sTemp & """ " & """" & sRegPath & =
""""
oScriptShell.Run sCmd, 0, True
RegExport =3D sTemp
End Function
'-----------------------------------------
Function NotZipped (oFolder)
NotZipped =3D Not (CBool (InStr (oFolder.type, "zipped")))
End Function
'-----------------------------------------
Function VPath (oFolder)
on error resume next
sTemp =3D oFolder.ParentFolder.title
If Err.Number =3D 0 then=20
VPath =3D VPath (oFolder.ParentFolder) & "\" & oFolder.title
Else
VPath =3D oFolder.title
Err.Clear
End If
End Function
'-----------------------------------------
Function NotAlreadySaved (oFolder)
Dim sVPath
NotAlreadySaved =3D True
sVPath =3D VPath (oFolder.GetFolder)
For Each s in VPathCollection
If s =3D sVPath Then
NotAlreadySaved =3D False
Exit For
End If
Next
End Function
'-----------------------------------------
Sub InitMsgStrs ()
sIntroMsg =3D "This script will allow you to customize the view for =
a folder and " & _
"then have that view applied to all of the folder's subfolders. =
" & _
"All previously saved views will be lost. Your old settings " & =
_
"will be saved in two .reg files placed on the desktop that can =
be " & _
"merged to restore your old settings." & vbCrLf & vbCrLf & _
"Work your way up from lower-level folders to higher-level " & _
"folders -- i.e. do My Music and My Pictures before doing My =
Documents. " & _
"If a sub-folder has already had a view saved, It will not be =
affected by " & _
"applying a different view to its parent folder." & vbCrLf & =
vbCrLf & _
"Also, different views are saved for 'My Documents' and its " & =
_
"subfolders, depending on how they are accessed:" & vbCrLf & =
vbCrLf & _
vbTab & "'Desktop\My Documents'" & vbCrLf & vbTab & "'My =
Computer\My Documents'" & _
vbCrLf & vbTab & "'C:\Docs and Settings\<username>\My =
Documents'" & vbCrLf & _
vbCrLf & "are seperate and unique as far as views are concerned. =
You may want " & _
"to take a minute to verify which way you access these folders =
by opening one " & _
"and viewing the folder pane to determine the path." & vbCrLf & =
vbCrLf & _
"Do you wish to continue?"
sScriptTitle =3D "Customize Folder Views"
sIntro2 =3D "This script will be opening and closing explorer =
windows in order to " & _
"apply the views. It is recommended you close other =
applications to run " & _
"this Script. To function properly, It is IMPERATIVE that you =
not have " & _
"any Explorer or I.E. windows open. If you have any open, =
please close " & _
"them before clicking 'OK'" & vbCrLf & vbCrLf & "Do you wish to =
continue?"
sSetView =3D "Alt+Tab to the Folder window now open and set all view =
options to your " & _
"liking. DO NOT CLOSE THE FOLDER YOURSELF! When you are done, =
Alt+Tab back to this window & click 'OK'."=20
End Sub
------=_NextPart_000_01A3_01C526AA.CB4F12E0
Content-Type: text/plain;
name="FolderViewSave.txt"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="FolderViewSave.txt"
Const conBagMRUPath =3D =
"HKEY_CURRENT_USER\Software\Microsoft\Windows\ShellNoRoam\BagMRU"
Const conBagsPath =3D =
"HKEY_CURRENT_USER\Software\Microsoft\Windows\ShellNoRoam\Bags"
Const conFolderStreams =3D =
"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Streams\Defaults=
\{F3364BA0-65B9-11CE-A9BA-00AA0=AD04AE837}"
Const ForReading =3D 1
dim oAppShell, oScriptShell, oFso, oFolder
dim VPathCollection ()
dim sIntroMsg, sScriptTitle, sIntro2, sSetView
'Instructions & Warnings
InitMsgStrs
If (msgbox(sIntroMsg, vbYesNo, sScriptTitle) <> vbYes) Then
WScript.Quit
End If
If (msgbox(sIntro2, vbYesNo + vbCritical, sScriptTitle) <> vbYes) Then
WScript.Quit
End If
'Assign objects
Set oAppShell =3D CreateObject("Shell.Application")
Set oScriptShell =3D CreateObject("WScript.Shell")=20
Set oFSO =3D CreateObject("Scripting.FileSystemObject")
'Verify other windows are closed
Do While oAppShell.windows.count <> 0
If (msgbox(oAppShell.windows.count & " Explorer/I.E. windows are =
still open!", _
vbRetryCancel + vbCritical, sScriptTitle) <> vbReTry) Then
WScript.Quit
End If
Loop
=20
'Back up the Registry Keys
Call RegExport(conBagMRUPath, "BagMRU Backup.reg")
Call RegExport(conBagsPath, "Bags Backup.reg")
DeleteBagsAndIncreaseLimit
'If conFolderStreams exists,it screws up the
' default columns for detail view of special folders. So...
On Error Resume Next
oScriptShell.RegDelete conFolderStreams
Err.Clear
On Error Goto 0
iNewBagCount =3D 0
bContinue =3D True
iLoopCount =3D 0
Do While bContinue '*** The Fun Starts Here ***
iLoopCount =3D iLoopCount + 1
set oFolder =3D oAppShell.BrowseForFolder(0, "Choose a Folder", 0)
oAppShell.Open oFolder
WScript.Sleep 1000
msgbox sSetView,,oFolder.title & " - Script Paused..."
oAppShell.windows.item.quit
iNewBagCount =3D iNewBagCount + 1
ReDim Preserve VPathCollection(iLoopCount - 1)
VPathCollection(iLoopCount - 1) =3D VPath(oFolder)
sFolderTemplateFile =3D MakeFolderTemplate(iNewBagCount)
RegImport sFolderTemplateFile
sStart =3D Now
iNewBagCount =3D iNewBagCount + OpenAndCloseSubfolders(oFolder)
MsgBox "Finished in " & DateDiff("s", sStart, Now) & " seconds", _=20
vbSystemModal, "OpenAndCloseSubfolders"
oFso.GetFile(sFolderTemplateFile).Delete
oScriptShell.RegDelete(conBagsPath & "\AllFolders\Shell\")
oScriptShell.RegDelete(conBagsPath & "\AllFolders\")
msg =3D "You have saved views for the following folder(s):" & vbCrLf
For each sPath in VPathCollection
msg =3D msg & vbTab & sPath & vbCrLf
Next
msg =3D msg & "You have saved views for " & iNewBagCount & " folders =
(8000 max)" & vbCrLf
msg =3D msg & vbCrLf & "Would you like to do another folder?"
bcontinue =3D msgbox (msg, vbYesNo) =3D vbYes
Loop
Set oAppShell =3D Nothing
Set oScriptShell =3D Nothing
Set oFSO =3D Nothing
'---------------------------------------End------------------------------=
-----------
Function MakeFolderTemplate (iBagNum)
Dim sTemplateRegPath, sTemplateFileName, sFolderTemplate, ts
sTemplateRegPath =3D conBagsPath & "\" & iBagNum
sTemplateFileName =3D "Folder Template" & iBagNum & ".reg"
sFolderTemplate =3D RegExport (sTemplateRegPath, sTemplateFileName)
'msgbox sFolderTemplate
Set ts =3D oFso.OpenTextFile(sFolderTemplate, ForReading)
sTemplateInfo =3D ts.ReadAll
ts.close
sSearch =3D "Bags\" & iNewBagCount
sReplace =3D "Bags\AllFolders"
sTemplateInfo =3D Replace (sTemplateInfo, sSearch, sReplace, 1, 2)
sTemplateInfo =3D Replace (sTemplateInfo, "My", "")
Set ts =3D oFso.CreateTextFile (sFolderTemplate, true)
ts.Write sTemplateInfo
ts.Close
Set ts =3D Nothing
MakeFolderTemplate =3D sFolderTemplate =20
End Function
'----------------------------------------
Function OpenAndCloseSubfolders (oFolder)
Dim iCount
iCount=3D0
For Each oItem in oFolder.Items
If oItem.IsFolder Then
If NotZipped(oItem) and NotAlreadySaved(oItem) Then
'Do Until oAppshell.windows.count =3D 0
' WScript.Sleep 100
'Loop
oAppshell.open oItem.GetFolder
'WScript.Sleep 1000
On Error Resume Next
Do
Err.Clear
oAppshell.windows.item.quit
Loop Until Err.Number =3D 0
iCount =3D iCount + 1
iCount =3D iCount + =
OpenAndCloseSubfolders(oItem.GetFolder)
End If
End If
Next
OpenAndCloseSubfolders =3D iCount
End Function
'-----------------------------------------
Sub DeleteBagsAndIncreaseLimit ()
Dim tFolder, tName, tFile, tString
Const TemporaryFolder =3D 2
Set tFolder =3D oFso.GetSpecialFolder(TemporaryFolder)
tName =3D oFso.GetTempName =20
Set tFile =3D tFolder.CreateTextFile(tName)
tString =3D _
"REGEDIT4" & vbCrLf & vbCrLf & _
"[-HKEY_CURRENT_USER\Software\Microsoft\Windows\ShellNoRoam\BagMRU]" =
& vbCrLf & _
"[-HKEY_CURRENT_USER\Software\Microsoft\Windows\ShellNoRoam\Bags]" & =
vbCrLf & vbCrLf & _
"[HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell]" & vbCrLf & _
"""BagMRU Size""=3Ddword:00001f40" & vbCrLf & vbCrLf & _
"[HKEY_CURRENT_USER\Software\Microsoft\Windows\ShellNoRoam]" & =
vbCrLf & _
"""BagMRU Size""=3Ddword:00001f40" & vbCrLf & vbCrLf & _
=
"[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Ex=
plorer]" & vbCrLf & _
"""NoSaveSettings""=3Ddword:00000000" & vbCrLf & vbCrLf
tFile.write (tString)
tFile.close
RegImport (tFolder.Path & "\" & tName)
oFso.GetFile(tFolder.Path & "\" & tName).Delete
End Sub =20
'-----------------------------------------
Sub RegImport (sFileName)
Dim sCmd
sCmd =3D "regedit.exe /s """ & sFileName & """"
oScriptShell.Run sCmd, 0, True
End Sub
'-----------------------------------------
Function RegExport (sRegPath, sFileName)
Dim sCmd, sTemp
sTemp =3D oScriptShell.SpecialFolders("Desktop") & "\" & sFileName
sCmd =3D "regedit.exe /A """ & sTemp & """ " & """" & sRegPath & =
""""
oScriptShell.Run sCmd, 0, True
RegExport =3D sTemp
End Function
'-----------------------------------------
Function NotZipped (oFolder)
NotZipped =3D Not (CBool (InStr (oFolder.type, "zipped")))
End Function
'-----------------------------------------
Function VPath (oFolder)
on error resume next
sTemp =3D oFolder.ParentFolder.title
If Err.Number =3D 0 then=20
VPath =3D VPath (oFolder.ParentFolder) & "\" & oFolder.title
Else
VPath =3D oFolder.title
Err.Clear
End If
End Function
'-----------------------------------------
Function NotAlreadySaved (oFolder)
Dim sVPath
NotAlreadySaved =3D True
sVPath =3D VPath (oFolder.GetFolder)
For Each s in VPathCollection
If s =3D sVPath Then
NotAlreadySaved =3D False
Exit For
End If
Next =20
End Function
'-----------------------------------------
Sub InitMsgStrs ()
sIntroMsg =3D "This script will allow you to customize the view for =
a folder and " & _
"then have that view applied to all of the folder's subfolders. =
" & _
"All previously saved views will be lost. Your old settings " & =
_
"will be saved in two .reg files placed on the desktop that can =
be " & _
"merged to restore your old settings." & vbCrLf & vbCrLf & _
"Work your way up from lower-level folders to higher-level " & _
"folders -- i.e. do My Music and My Pictures before doing My =
Documents. " & _
"If a sub-folder has already had a view saved, It will not be =
affected by " & _
"applying a different view to its parent folder." & vbCrLf & =
vbCrLf & _
"Also, different views are saved for 'My Documents' and its " & =
_
"subfolders, depending on how they are accessed:" & vbCrLf & =
vbCrLf & _
vbTab & "'Desktop\My Documents'" & vbCrLf & vbTab & "'My =
Computer\My Documents'" & _
vbCrLf & vbTab & "'C:\Docs and Settings\<username>\My =
Documents'" & vbCrLf & _
vbCrLf & "are seperate and unique as far as views are concerned. =
You may want " & _
"to take a minute to verify which way you access these folders =
by opening one " & _
"and viewing the folder pane to determine the path." & vbCrLf & =
vbCrLf & _
"Do you wish to continue?"
sScriptTitle =3D "Customize Folder Views"
sIntro2 =3D "This script will be opening and closing explorer =
windows in order to " & _
"apply the views. It is recommended you close other =
applications to run " & _
"this Script. To function properly, It is IMPERATIVE that you =
not have " & _
"any Explorer or I.E. windows open. If you have any open, =
please close " & _
"them before clicking 'OK'" & vbCrLf & vbCrLf & "Do you wish to =
continue?"
sSetView =3D "Alt+Tab to the Folder window now open and set all view =
options to your " & _
"liking. DO NOT CLOSE THE FOLDER YOURSELF! When you are done, =
Alt+Tab back to this window & click 'OK'."=20
End Sub
------=_NextPart_000_01A3_01C526AA.CB4F12E0--