Jump to content

Determine if LNK file refers to file or folder


leuce
 Share

Recommended Posts

G'day everyone

I'm writing a script to process some LNK files, but it is important to me that the script's actions are performed only if the LNK file links to an actual file, and not to a e.g. folder or a control panel item or suchlike. Is there a way to determine whether a full path refers to an actual file?

Essentially, what I want to do is to copy the linked file from its existing location to the location of the LNK file (after I used a rather too aggressive setting on a duplicate remover program).

My initial script has an @error thing (what do you call that?) that should tell me whether the file copy succeeded, but if the LNK file links to a folder instead of a file, the script tells me that the copy was a success, even though thankfully it doesn't copy the folder. I do not want it to copy folders anyway.

Here is my current script, in case you're interested, although I don't think it is needed to answer my query:

$lnkfile = FULL PATH TO LNK FILE
$lnkfilefolder = StringRegExpReplace ($lnkfile, "\\[^\\]*$", "")
$lnkdetails = FileGetShortcut($lnkfile)

If Not @error Then
$linkedfile = $lnkdetails[0]
FileCopy ($linkedfile, $lnkfilefolder)

If Not @error Then
FileMove (StringTrimRight ($lnkfile, 4) & ".lnk", StringTrimRight ($lnkfile, 4) & ".copied_yes.lnk")
Else
FileMove (StringTrimRight ($lnkfile, 4) & ".lnk", StringTrimRight ($lnkfile, 4) & ".copied_no.lnk")
EndIf

EndIf

Any ideas about how to do this?

Thanks

Samuel

Link to comment
Share on other sites

One thing I could do, which works, is to try to change the working directory to the linked full path, and if it fails, then I know that it aint a folder.

Would this be a safe thing to do, though? Should I reverse it, to be on the safe side (i.e. first determine the current working directory, then after the test change the working directory back to what it was)?

[Edited: no, I tried that and it didn't work.]

Samuel

Edited by leuce
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...