View Full Version : My IF..Then is'nt working (All)
redlair
05-28-2004, 12:48 PM
Hello and Help..
My copy file statement works fine but the IF statement that is to look and verify there are links on the desktop is not working correctly. The Idea is to look and see if there are any .lnk files on the desktop and then copy them into a folder on the desktop.
DesktopPath = Shell.SpecialFolders("Desktop")
If Not FSO.FileExists(DesktopPath & "\*.lnk") Then
FSO.CopyFile DesktopPath & "\*.lnk", DesktopPath & "\XP Upgrade " & Suser
End If
cprasley
05-30-2004, 06:24 AM
I've never seen FileExists() used with wildcards, and suspect it may be taking the filespec literally (looking for the file "*.lnk", which can't exist).
The work-around would be to enumerate the directory contents and compare each filename using a regular expression search.
DesktopPath = Shell.SpecialFolders("Desktop")
Set wanted = new RegExp
wanted.pattern = ".*\.lnk"
Set fso = CreateObject ("Scripting.FileSystemObject")
Set f = fso.GetFolder(DesktopPath)
Set fc = f.Files
For Each f1 in fc
if wanted.test(f1.name) then
fso.CopyFile DesktopPath & "\*.lnk", DesktopPath & "\XP Upgrade " & Suser
endif
Next
redlair
05-30-2004, 10:16 AM
Thank-You, That took care of the problems. Could I get a run down on what this code is doing.
Set wanted = new RegExp
wanted.pattern = ".*\.lnk"
Set fso = CreateObject ("Scripting.FileSystemObject")
Set f = fso.GetFolder(DesktopPath)
Set fc = f.Files
For Each f1 in fc
if wanted.test(f1.name) then
cprasley
06-07-2004, 10:16 PM
; Create a "Regular Expression" object.
Set wanted = new RegExp
; . means "match any character"
; * means "apply the previous match zero or more times"
; \ means the next character is special or literal (depending on what that next character is).
; In this case, \. means the "." is a real "." and not a match-anything character. "\n" would be a carriage return, for example.
; So our pattern says "match anything followed by a period and the characters "lnk".
wanted.pattern = ".*\.lnk"
; The FileSystemObject is how we interact with the operating system.
Set fso = CreateObject ("Scripting.FileSystemObject")
; Ask FileSystemObject to read the Desktop directory.
Set f = fso.GetFolder(DesktopPath)
; A directory contains lots of extra information, but all we want is the list of files (in a collection variable).
Set fc = f.Files
; Iterate through the file collection. f1 will contain information about each file in turn.
For Each f1 in fc
; We get the file name from 'f1.name' and compare it to our target using a RegExp test() function.
if wanted.test(f1.name) then
; and if it matches then we do something.
redlair
06-08-2004, 07:34 AM
Thank you...very Much!!
Powered by vBulletin™ Version 4.1.0 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.