{"id":55893,"date":"2008-03-29T00:31:00","date_gmt":"2008-03-29T00:31:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/03\/29\/hey-scripting-guy-how-can-i-retrieve-information-about-all-the-shortcut-files-in-a-folder\/"},"modified":"2008-03-29T00:31:00","modified_gmt":"2008-03-29T00:31:00","slug":"hey-scripting-guy-how-can-i-retrieve-information-about-all-the-shortcut-files-in-a-folder","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-retrieve-information-about-all-the-shortcut-files-in-a-folder\/","title":{"rendered":"Hey, Scripting Guy! How Can I Retrieve Information About All the Shortcut Files in a Folder?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/> <\/p>\n<p>Hey, Scripting Guy! I have a folder with about 1800 shortcuts in it. I need to retrieve the file name and the target for each of these shortcuts in order to import that information into a database. Do you have any idea how I can retrieve this information for each and every shortcut file in a folder?<\/p>\n<p>&#8212; JK<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\" \/><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\" \/><\/a> <\/p>\n<p>HeYy, JK. WwEELL, PRobbaly THe bESS \u2013 sorry; it\u2019s HArdd to tyBE wiiith middEns ON.<\/p>\n<p>Sorry; like we said (or tried to say) it\u2019s hard to type with middens \u2013 uh, <i>mittens<\/i> \u2013 on. Anyway, probably the best way to \u2013<\/p>\n<p>What\u2019s that? Why <i>is<\/i> the Scripting Guy who writes this column typing with mittens on in the first place? Well, truth be told, this column is being written on a Thursday morning, and the Scripting Guy who\u2019s trying to write this column is still trying to warm up after sitting outside watching high school baseball on Wednesday night. Officially, the game-time temperature last night was 39 degrees Fahrenheit; however, thanks to the wind and the rain it felt a bit colder than that. How cold? Off the top of our heads, we\u2019d say around 116 degrees below zero.<\/p>\n<p>OK, so that <i>is<\/i> a bit of an exaggeration. In reality, it felt no colder than 103 or 104 degrees below zero. That leaves us a bit short of the coldest temperature ever registered on Earth: 128.6 degrees below zero, recorded at Vostok Station, Antarctica. The coldest temperature ever recorded in the state of Washington is minus 48 degrees Fahrenheit; fortunately that occurred in December of 1968, when there were no high school baseball games scheduled. Interestingly enough, the coldest temperature ever recorded in Hawaii is 12 degrees <i>above<\/i> zero. And even that was at the top of Mt. Mauna Kea, elevation 13,770 feet. <\/p>\n<p>Yes, we wish <i>we<\/i> were in Hawaii right about now, too.<\/p>\n<table class=\"dataTable\" id=\"ESD\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Note<\/b>. Did we mention the fact that there were a few flakes of <i>snow<\/i> mixed in with the rain? Well, we should have, because there were. And that was fun; after all, you hardly ever get to see snow when you\u2019re at a baseball game.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Of course, some of you may be thinking, \u201cSitting in the rain, wind, and cold and watching a high school baseball game? That doesn\u2019t sound so bad.\u201d To those of you who feel that way all we can say is this: the customer is always right.<\/p>\n<p>Except in this case, when you\u2019re 100% <i>wrong<\/i>: sitting in the rain, wind, and cold and watching a high school baseball game is absolutely no fun at all. No matter how much you like baseball. And regardless of whether you\u2019re a Microsoft customer or not.<\/p>\n<p>So just how cold <i>was<\/i> it last night? Well, you might say that it was colder than a witch\u2019s \u2013 um, let\u2019s just say that it was cold. Really cold. <\/p>\n<p>Fortunately, though, things are a little better today; that\u2019s because the Scripting Guy who writes this column is safely-cocooned inside his climate-controlled office.<\/p>\n<table class=\"dataTable\" id=\"EME\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Note<\/b>. Well, pseudo-climate controlled. A year or so ago Microsoft installed individual thermostats in each office, but they appear to be placebos rather than actual thermostats. As near as we can tell, it doesn\u2019t matter whether you set the thermostats to 40 degrees or 140 degrees; the temperature in your office remains exactly the same either way.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>In fact, the Scripting Guy who writes this column has even thawed out enough to hammer out a script that can retrieve the file name and target for all the shortcut files in a specified folder:<\/p>\n<pre class=\"codeSample\">strComputer = \".\"\n\nSet objWMIService = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")\n\nSet colShortcuts = objWMIService.ExecQuery(\"Select * From Win32_ShortcutFile Where \" &amp; _\n    \"Drive = 'c:' AND Path = '\\\\documents and settings\\\\all users\\\\shortcut_files\\\\'\")\n\nFor Each objShortcut in colShortcuts\n    Wscript.Echo objShortcut.FileName\n    Wscript.Echo objShortcut.Name\n    Wscript.Echo objShortcut.Target\n    Wscript.Echo\nNext\n<\/pre>\n<p>Adkab dsgjdsa fli;<\/p>\n<p>Sorry; the stocking cap dropped down over our eyes again. As you can see (assuming you don\u2019t have a stocking cap covering <i>your<\/i> eyes) we start out by connecting to the WMI service on the local computer. Could we run this script against a <i>remote<\/i> computer? Of course we could; to do that we\u2019d simply need to assign the name of the remote computer to the variable strComputer:<\/p>\n<pre class=\"codeSample\">strComputer = \"atl-fs-001\"\n<\/pre>\n<p>After connecting to the WMI service we then use the following query to return a collection of all the shortcut files found in the folder C:\\Documents and Settings\\All Users\\Shortcut_Files:<\/p>\n<pre class=\"codeSample\">Set colShortcuts = objWMIService.ExecQuery(\"Select * From Win32_ShortcutFile Where \" &amp; _\n    \"Drive = 'c:' AND Path = '\\\\documents and settings\\\\all users\\\\shortcut_files\\\\'\")\n<\/pre>\n<p>We should probably take a moment to examine this query in a little more depth. To begin with, note that we\u2019re selecting items from the <b>Win32_ShortcutFile<\/b> class; as the name implies, this class contains instances of all the shortcut files found on the computer. In addition, we\u2019ve added a Where clause with the following two conditions: 1) any returned files must be found on drive C (<b>Drive = &#8216;c:&#8217;<\/b>)<i>; <\/i>and, 2) any returned files must be found in the folder Documents and Settings\\All Users\\Shortcut_Files. <\/p>\n<p>For better or worse, this Where clause isn\u2019t as straightforward as it could be. For one thing, we need to split the clause into two pieces: the drive must be referenced separately from the folder path. WMI won\u2019t let you request files from the folder C:\\Documents and Settings\\All Users\\Shortcut_Files; instead, you have to request files from drive C <i>and<\/i> the folder \\Documents and Settings\\All Users\\Shortcut_Files (the complete path minus the drive letter).<\/p>\n<p>Strange, but true.<\/p>\n<p>Second, note that we have to double up each and every \\ in the folder path: <\/p>\n<pre class=\"codeSample\">\\\\documents and settings\\\\all users\\\\shortcut_files\\\\\n<\/pre>\n<p>Why? Well, as it turns out, the \\ is a reserved character in WMI; in order to use a \\ in a WMI query each such character must be \u201cescaped.\u201d Because the \\ is also the escape character in WMI, that means we need to preface each \\ with a second \\. Thus a path like \\Scripts\\My Scripts\\WMI\\ has to look like this when used in a WMI query:<\/p>\n<pre class=\"codeSample\">\\\\Scripts\\\\My Scripts\\\\WMI\\\\\n<\/pre>\n<p>Like we said: strange, but true.<\/p>\n<p>Once we\u2019ve nailed down the query the rest of the script is a piece of cake. After we get back a collection of all the shortcut files found in C:\\Documents and Settings\\All Users\\Shortcut_Files we set up a For Each loop to loop through each item in that collection. Inside the loop we use the following block of code to echo back the shortcut <b>FileName<\/b> (the name of the file, minus the file extension), <b>Name<\/b> (complete file path, including the drive, folder, and file name and extension), and <b>Target<\/b> (the application\/script\/whatever that gets called any time the shortcut is clicked):<\/p>\n<pre class=\"codeSample\">Wscript.Echo objShortcut.FileName\nWscript.Echo objShortcut.Name\nWscript.Echo objShortcut.Target\nWscript.Echo\n<\/pre>\n<p>From there it\u2019s back to the top of the loop, where we repeat the process with the next shortcut in the collection. When we\u2019re all done we should have an onscreen report that looks something like this:<\/p>\n<pre class=\"codeSample\">InterVideo WinDVD\nc:\\documents and settings\\all users\\shortcut_files\\intervideo windvd.lnk\nC:\\Program Files\\InterVideo\\WinDVD\\WinDVD.exe\n\nIT Connection Manager\nc:\\documents and settings\\all users\\shortcut_files\\connection manager.lnk\nC:\\Program Files\\InterVideo\\WinDVD\\WinDVD.exe\n\nZune\nc:\\documents and settings\\all users\\shortcut_files\\zune.lnk\nC:\\Program Files\\Zune\\Zune.exe\n<\/pre>\n<p>Etc., etc.<\/p>\n<p>That should do it, JK. As for the Scripting Guy who writes this column, he\u2019s finally beginning to warm up. Which is good: by the time this column is published (Friday, March 28) he\u2019ll be getting ready to head for yet another baseball game, an afternoon game to be played in Seattle. Here\u2019s the current forecast for Friday afternoon:<\/p>\n<table class=\"dataTable\" id=\"ELG\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">Periods of rain. Becoming windy in the afternoon. High 43F. SSE winds at 5 to 10 mph, increasing to 20 to 30 mph. Chance of rain 90%. Rainfall near a half an inch.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>A high of 43, winds up to 30 miles an hour, and a 90% chance of rain. Watch out, Vostok Station, Antarctica. Your days as a world record holder just might be numbered.<\/p>\n<p>Speaking of which, where did we put those middens?<\/p>\n<p>Sorry; <i>mittens<\/i>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a folder with about 1800 shortcuts in it. I need to retrieve the file name and the target for each of these shortcuts in order to import that information into a database. Do you have any idea how I can retrieve this information for each and every shortcut file in [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[38,11,3,12,5],"class_list":["post-55893","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-folders","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have a folder with about 1800 shortcuts in it. I need to retrieve the file name and the target for each of these shortcuts in order to import that information into a database. Do you have any idea how I can retrieve this information for each and every shortcut file in [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=55893"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55893\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=55893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}