2011-04-12 82 views
0

此腳本的總體目標: 發送給員工並向每檯筆記本電腦添加一組收藏夾(網絡文件夾)。到目前爲止,我已經編寫了一個腳本,用於將所有文件夾添加到收藏夾中,但它們在收藏夾列表的最後被刪除。.vbs對用戶列表進行排序收藏夾

我正在尋找一種按日期排序收藏夾列表的方法,或者甚至更好地在列表頂部插入文件夾,以便它不會更改當前用戶收藏夾的順序。

這是我到目前爲止,鏈接無法正常工作,因爲他們是我們的內部網

'Create Network Folder******************************************* 


Option Explicit 
MsgBox("Click OK to begin installing all of the Web Folders under My Network Places.") 


Sub CreateNetworkFolder(siteURL, siteName) 

Dim iRes, jRes, MT, TT 
Dim SH, newPath 
Dim objFso, f, fs, g 

Dim bString 
Dim ltrIndex 
Dim nameLength, urlLength, urlCutoff 
Dim aFile 
Dim filesys 

'ForWriting (2) is the attribute to be set when writing to a file. 
Const ForWriting = 2 

nameLength = Len(siteName) 
urlLength = Len(siteURL) 
'44 seems to be the length where we have to change a 00 to a 01. 
urlCutoff = 44 

Set objFso = CreateObject("Scripting.FileSystemObject") 

Set SH = WScript.CreateObject("WScript.Shell") 

'Create the folder under Favorites that will hold the target.lnk file 
newPath = SH.SpecialFolders("Favorites") & "\" & "BCKM" & "\" 

'Creating the housing folder for BCKM 
Set filesys = CreateObject("Scripting.FileSystemObject") 

If filesys.FolderExists(newPath) Then 
    'msgbox "File esists" 
    else 
    'msgbox "file does not exist" 
    objFso.CreateFolder(newPath) 
End If 



'Create the webfolder for each section 
newPath = SH.SpecialFolders("Favorites") & "\" & "BCKM" & "\" & siteName 
objFso.CreateFolder(newPath) 

'We ceate a Desktop.ini file 
Set fs = CreateObject("Scripting.FileSystemObject") 
aFile = newPath & "\Desktop.ini" 

Set f = fs.OpenTextFile(aFile, ForWriting, True) 

'Write the data lines that will make this a folder shortcut. 
f.WriteLine "[.ShellClassInfo]" 
f.WriteLine "CLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D}" 
f.WriteLine "Flags=2" 
f.WriteLine "ConfirmFileOp=0" 



f.Close 

'We make Desktop.ini a system-hidden file by assigning it attribute of 6 
Set fs = CreateObject("Scripting.FileSystemObject") 
Set g = fs.GetFile(newPath & "\Desktop.ini") 
g.Attributes = 6 

'We make the folder read-only by assigning it 1. 
Set fs = CreateObject("Scripting.FileSystemObject") 
Set g = fs.GetFolder(newPath) 
g.Attributes = 1 

'This is where we construct the target.lnk file byte by byte. Most of 
'the lines are shown in 16 byte chunks, 
'mostly because that is the way I saw it in the Debug utility I was 
'using to inspect shortcut files. 

'Line 1, 16 bytes 
bString = Chr(&H4C) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H01) & Chr(&H14) & Chr(&H02) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&HC0) & Chr(&H00) & Chr(&H00) & Chr(&H00) 

'Line 2, 16 bytes 
bString = bString & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H46) & Chr(&H81) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) 

'Line 3, 16 bytes 
bString = bString & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) 

'Line 4., 16 bytes. 13th byte is significant. 
bString = bString & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H01) & Chr(&H00) & Chr(&H00) & Chr(&H00) 

'Line 5. 13th byte is significant. 
bString = bString & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) 

'When I was analyzing the next byte of shortcuts I created, I found 
'that it is set to various values, 
'and I have no idea what they are referring to. In desperation I tried 
'substituting some values. 
'00 caused a crash of Explorer. FF seeems to work fine for all. 
'If anyone can get back to me on what this byte is or why FF works, 
'please contact me. 
bString = bString & Chr(&HFF) 

'This byte is 00 if the URL is 44 characters or less, 01 if greater. 
If urlLength > urlCutoff Then 
bString = bString & Chr(&H01) 
Else 
bString = bString & Chr(&H00) 
End If 

bString = bString & Chr(&H14) & Chr(&H00) 

'Line 6, 16 bytes 
bString = bString & Chr(&H1F) & Chr(&H50) & Chr(&HE0) & Chr(&H4F) & Chr(&HD0) & Chr(&H20) & Chr(&HEA) & Chr(&H3A) & Chr(&H69) & Chr(&H10) & Chr(&HA2) & Chr(&HD8) & Chr(&H08) & Chr(&H00) & Chr(&H2B) & Chr(&H30) 

'Line 7, 16 bytes 
bString = bString & Chr(&H30) & Chr(&H9D) & Chr(&H14) & Chr(&H00) & Chr(&H2E) & Chr(&H00) & Chr(&H00) & Chr(&HDF) & Chr(&HEA) & Chr(&HBD) & Chr(&H65) & Chr(&HC2) & Chr(&HD0) & Chr(&H11) & Chr(&HBC) & Chr(&HED) 

'Line 8, 16 bytes 
bString = bString & Chr(&H00) & Chr(&HA0) & Chr(&HC9) & Chr(&H0A) & Chr(&HB5) & Chr(&H0F) & Chr(&HA4) 

'This byte is 00 if the URL is 44 characters or less, 01 if greater. 
If urlLength > urlCutoff Then 
bString = bString & Chr(&H01) 
Else 
bString = bString & Chr(&H00) 
End If 

bString = bString & Chr(&H4C) & Chr(&H50) & Chr(&H00) & Chr(&H01) & Chr(&H42) & Chr(&H57) & Chr(&H00) & Chr(&H00) 

'Line 9, 16 bytes 
bString = bString & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H10) & Chr(&H00) 

'Line 10, 2 bytes 
bString = bString & Chr(&H00) & Chr(&H00) 

'The next byte represents the length of the site name. 
bString = bString & Chr(nameLength) 

'Take the site name, and write each letter, preceeded by a "00" 
'character. 

For ltrIndex = 1 to nameLength 
bString = bString & Chr(&H00) & Mid(siteName, ltrIndex, 1) 
Next 

'Middle line, separates the Folder Name from the URL. 3 bytes. 
bString = bString & Chr(&H00) & Chr(&H00) & Chr(&H00) 

'The next byte represents the length of the site URL. 
bString = bString & Chr(urlLength) 

'Take the site URL, and write each letter, preceeded by a "00" 
'character. 
For ltrIndex = 1 to urlLength 
bString = bString & Chr(&H00) & Mid(siteURL, ltrIndex, 1) 
Next 

'Last line, 13 bytes 
bString = bString & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) & Chr(&H00) 

'Let's create the target.lnk file. 
Set fs = CreateObject("Scripting.FileSystemObject") 
aFile = newPath & "\target.lnk" 
'aFile = newPath & "\vb.sss" 
Set f = fs.OpenTextFile(aFile, ForWriting, True) 
f.Write bString 
f.Close 

End Sub 

'This is where you specify the folders to add 

CreateNetworkFolder "http://collaboration.au.xxx.com/dav/1.11.685732/", "Clients" 
CreateNetworkFolder "http://collaboration.au.xxx.com/dav/1.11.685930/", "Workspaces" 
CreateNetworkFolder "http://collaboration.au.xxx.com/dav/1.11.685941/", "Practice Management" 
CreateNetworkFolder "http://collaboration.au.xxx.com/dav/1.11.685945/", "Tips" 
CreateNetworkFolder "http://collaboration.au.xxx.com/dav/1.21.8015/", "Discussion Board" 


MsgBox("All Web Folders Added Succesfully. Please check Favorites to Confirm. Press OK") 

回答

0

首先,你爲什麼在這樣一個奇怪的方式創建快捷方式?通過調用WshShell.CreateShortcut並設置生成的WshShortcut對象的屬性,可以輕鬆完成此操作。


現在到你的排序問題。有兩個管理收藏夾狀態的註冊表項:

  • HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MenuOrder\Favorites - 對應於Internet Explorer中顯示的收藏夾。
  • HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MenuOrder\Favorites - 對應於Windows開始菜單>收藏夾中顯示的收藏夾。

收藏夾的排序順序存儲在這些鍵的DWORD Order值中。對於收藏夾中的每個文件夾,都有相應的子鍵,其值也是OrderOrder值的內部結構沒有記錄,但this article提供了一些分析和解釋。

因此,要更改排序順序,您可以嘗試從腳本修改此值,例如,使用WMI StdRegProv.GetDWORDValueStdRegProv.SetDWORDValue方法。不過,我想這會很棘手。

請注意,從註冊表中刪除這些上述密鑰應該是sort Favorites in alphabetical order,如果這是一個選項。

2

要跟蹤海倫的回答,註冊表項中的「未公開的數據」是相對PCUITEMID_CHILD(請參閱MSDN)的二進制形式 - 相對於.lnk文件存儲在磁盤上的文件夾。這需要使用可以訪問COM接口的語言,例如C++。

註冊表項具有文章中記錄的標題 - 但是,記錄只是記錄(DWORD),'order'值(DWORD)和PCUITEMID_CHILD數據的長度。

typedef struct _FAV_ORDERED_HEADER_ 
{ 
    DWORD dwFormatValue; // Typically 8 for known Windows versions that use this 
    DWORD dwSubFormatValue; // Typically 2 for known Windows that use this 
    DWORD dwTotalDataLen; // Total bytes in all data 
    DWORD dwReserved;  // No known use 
    DWORD dwNumRecords;  // Number of records 
} FAV_ORDERED_HEADER; 

typedef struct _FAV_ORDERED_RECORD_ 
{ 
    DWORD dwLenRecord;  // Length of each record (skip this bytes to get next record) 
    DWORD dwOrderValue;  // Negative if not ordered (add to end), positive if on list first 
    ITEMIDLIST pidlLinkData; // PIDL data for the individiual link - need to use a PIDL compare to be able to compare to actual .lnk file links. 
} FAV_ORDERED_RECORD; 
  1. 首先使用SHGetFolderLocation(NULL,CSIDL_FAVORITES,NULL,NULL,& ppidlFavorites)來獲取收藏夾中的PIDLIST_ABSOLUTE。

  2. 獲得通過SHGetDesktopFolder()桌面的IShellFolder接口指針

  3. 使用的IShellFolder實例才能到收藏夾的IShellFolder通過的IShellFolder :: BindToObject的(ppidlFavorites,NULL,IID_IShellFolder,& pFavoritesShellFolder)

  4. 通過調用帶有數據的IShellFolder :: GetDisplayNameOf()將pFavoritesShellFolder用於pidl參數 - 並設置SHGDNF參數以獲取字符串 - 顯示名稱,URL等通常通過此方法獲取。

這適用於Windows鏈接中的Windows 7/8'收藏夾'。然而,它需要使用IKnownFolderManager :: GetFolder(FOLDERID_Links,& pLinksKnFolder)來獲取IKnownFolder,它將允許最終檢索所需的IShellFolder - 正確獲取它的PIDLIST_ABSOLUTE。它是註冊表項(用於鏈接排序)是:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Modules\CommonPlaces