Solved

VBScript getting "file not found" during copy - but path and file are correct?

Posted on 2014-09-26
21
1,525 Views
Last Modified: 2014-10-10
Hello,

I have a vbscript that uses a "select case statement" to copy a specific file (a windows shortcut to a URL)  from one folder to another and rename it.

The first issue I keep hitting is that VBScript really seems to have a problem recognizing ".llnk" file extensions. Note that the following line only works if I use a wildecard ( * ) but if I use .lnk (which is the actual file name) the script cannot find the file and will not deleted it:

objFSO.DeleteFile "C:\scripts\shortcut\Picture Perfect 4 Client.*"

 Seems the only way I could get my original script to work was to use a wildcard instead of .lnk.  Now I need to use an actual file name with the .lnk.  If anyone can explain the deal with vb and .lnk it would be nice.

The big problem I have is that my script seems to work up to the copyfile point - it fails with "file not found" even though I have the exact path and filename.  Below is my code.  I've browed similar issues but none involved a rename so far.  Anyone know why this code is not seeing the file even though I have the right path and filename?  Again, I'm betting this has something to do with the file extension but I don't want to be dependent upon wildcards.

Select Case CommandResult

        Case "Primary"


            Set objFSO = CreateObject("Scripting.FileSystemObject")
            set WshShell = WScript.CreateObject("WScript.Shell")
'            tDesktopPath = WshShell.SpecialFolders("Desktop") & "\"
            objFSO.DeleteFile "C:\scripts\shortcut\Picture Perfect 4 Client.*"
            objFSO.CopyFile "C:\scripts\Server1Shortcuts\Server1 PP4 Client.lnk" , "C:\scripts\shortcut\Picture Perfect 4 Client.lnk"
0
Comment
Question by:yccdadmins
  • 10
  • 6
  • 5
21 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 40346251
Try getting a reference to the file using this method:

Set objFSO = CreateObject("Scripting.FileSystemObject")
set WshShell = WScript.CreateObject("WScript.Shell")
strFile = "C:\scripts\shortcut\Picture Perfect 4 Client.lnk"

if objfso.fileexists (strFile) Then
  set objFile = objFSO.GetFile (strFile)
  objFile.delete true
end if
objFSO.CopyFile "C:\scripts\Server1Shortcuts\Server1 PP4 Client.lnk" , "C:\scripts\shortcut\Picture Perfect 4 Client.lnk"

Open in new window

0
 

Author Comment

by:yccdadmins
ID: 40346291
I'll try this but I was trying to avoid if/else by deleting the existing file before the copy.  Give me a bit and I'll let you know how it worked....
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 250 total points
ID: 40346306
Any reason why you wouldn't just overwrite with the copyfile method?

Set objFSO = CreateObject("Scripting.FileSystemObject")
set WshShell = WScript.CreateObject("WScript.Shell")
strFile = "C:\scripts\shortcut\Picture Perfect 4 Client.lnk"
objFSO.CopyFile "C:\scripts\Server1Shortcuts\Server1 PP4 Client.lnk" , "C:\scripts\shortcut\Picture Perfect 4 Client.lnk", true

Open in new window

0
 

Author Comment

by:yccdadmins
ID: 40346347
Dang - I left the "true" off didn't I.   I'll add that back in but before the script even gets that far it is dying with "file not found"  It is dying on line 34, character 3.  That line in my script is:

objFSO.CopyFile "C:\scripts\BacsProdShortcuts\Bacsprod1 PP4 Client.lnk" , "C:\scripts\shortcut\Picture Perfect 4 Client.lnk", True

Not sure why I'm getting this error because that is the exact path and the exact filename.  It may have something to do with .lnk but I'm not sure.  I have this exact module of code working in another script but I had to use wildcard instead of .lnk for it to copy shortcuts to people's desktops.
0
 

Author Comment

by:yccdadmins
ID: 40346366
Just a quick FYI - I tested the code you posted and I received the same error - "file not found" even though the path and file are correct.

This has something to do with how the file is seen by the script.  Odd.

Still digging.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40346630
It is dying on line 34, character 3.  That line in my script is:

objFSO.CopyFile "C:\scripts\BacsProdShortcuts\Bacsprod1 PP4 Client.lnk" , "C:\scripts\shortcut\Picture Perfect 4 Client.lnk", True
With what error exactly?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40346639
Quite certain it's not the extension...does this produce the popup/status messages as indicated?
Any chance you could post the full script?

Set objFSO = CreateObject("Scripting.FileSystemObject")
set WshShell = WScript.CreateObject("WScript.Shell")
strSource = "C:\scripts\Server1Shortcuts\Server1 PP4 Client.lnk" 
strTarget = "C:\scripts\shortcut\Picture Perfect 4 Client.lnk"
if objFSO.FileExists(strSource) Then wscript.echo "Successfully found the source file!"
if objFSO.FileExists(strTarget) Then wscript.echo "Successfully found the target file!"
objFSO.CopyFile strSource, strTarget, true

Open in new window

0
 

Author Comment

by:yccdadmins
ID: 40354946
Sorry - had to pause on this for a while.  I will get back to it shortly...
0
 
LVL 12

Expert Comment

by:jkaios
ID: 40358517
Since there is a SPACE in the file name, you have to enclose it with double quotes by inserting chr(34) around the full path.

objFSO.DeleteFile chr(34) & "C:\scripts\shortcut\Picture Perfect 4 Client.lnk" & chr(34)
0
 

Author Comment

by:yccdadmins
ID: 40366050
Hello guys,

I've made a little headway.   The first errors I was getting actually did end up having to do with the file extension - I was using the wrong one.  I went into the registry and "unhit" lnk and url file extensions and found out I should have been using .url instead of .lnk.

So the code below seems to be working great except for the second part.
 
1.  The script deletes the existing file.
2. The script will copy the new file to the folder....but only if I leave a destination file name off.  If the destination is ""C:\scripts\shortcut\" the file is placed fine.  If I use ""C:\scripts\shortcut\Picture Perfect 4 Client.url" the file shows up with the correct name but it still contains the old content.  
3. Note that the purpose of copying the file is to change the URL destination from one server to another.

This works:

 Case "Primary"


            Set objFSO = CreateObject("Scripting.FileSystemObject")
            set WshShell = WScript.CreateObject("WScript.Shell")
            tDesktopPath = WshShell.SpecialFolders("Desktop") & "\"
            objFSO.DeleteFile "C:\scripts\shortcut\Picture Perfect 4 Client.url"
            objFSO.CopyFile "C:\scripts\ProdShortcuts\Server1 PP4 Client.url" , "C:\scripts\shortcut\", True

The following code still deletes the original file, placed a file with the correct name but it seems to be the same original file - the content does not change :

            Set objFSO = CreateObject("Scripting.FileSystemObject")
            set WshShell = WScript.CreateObject("WScript.Shell")
            tDesktopPath = WshShell.SpecialFolders("Desktop") & "\"
            objFSO.DeleteFile "C:\scripts\shortcut\Picture Perfect 4 Client.url"
            objFSO.CopyFile "C:\scripts\BacsProdShortcuts\Bacsprod1 PP4 Client.url" , "C:\scripts\shortcut\Picture Perfect 4 Client.url", True

I can get this to work using wildcards but I want to know why the CopyFile doesn't seem to be working....as intended that is.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 67

Expert Comment

by:sirbounty
ID: 40366585
Does this work?

strSource = "C:\scripts\BacsProdShortcuts\Bacsprod1 PP4 Client.url"
Set objSource = objFSO.GetFile strSource
strTarget = "C:\scripts\shortcut\Picture Perfect 4 Client.url"

objSource.Copy (strTarget)
0
 

Author Comment

by:yccdadmins
ID: 40367261
Thanks guys.  I basically don't have any more time to figure out why the file will not rename.  I changed the script so that it deletes the existing file using a wildcard and they uses CopyFile to copy the new file to a folder instead of a new filename.

In summary:

File not found - the first error that I was receiving (live 43, character 3) was caused because I was using the wrong file extension.  Once I changed .lnk to .url most everything started working.

The only thing that would not work was the CopyFile.  if I tried to copy from filename to another filename I received no errors but the content (URL)  in the file never changed.

Changed the script so that it deletes the existing file then copies the new file to the folder without trying to rename.

I would not consider this a resolution so I'm not sure how to assign the points....
0
 
LVL 12

Assisted Solution

by:jkaios
jkaios earned 250 total points
ID: 40367327
The following code still deletes the original file, placed a file with the correct name but it seems to be the same original file

It seems that this line doesn't work either:
  objFSO.DeleteFile "C:\scripts\shortcut\Picture Perfect 4 Client.url"

The DeleteFile and CopyFile functions both follow the Operating System's file naming convention where if there is a SPACE in the filename, then you must enclose it with double quotes.

Try the following DIR commands (note the difference) from the Command Prompt and see which one works:

1 - No quotes around the path:
  DIR C:\scripts\shortcut\Picture Perfect 4 Client.url

2 - Double quotes around the path
  DIR "C:\scripts\shortcut\Picture Perfect 4 Client.url"
0
 

Author Comment

by:yccdadmins
ID: 40367330
Interesting note on this.  I just found that the script is actually working but there is an oddity to it.

The script deletes the first file and then copies the new file.  To see if the script was successful I was going to the properties of the .lnk file and looking under the Web Document tab in the URL section to see if the string had changed and it never did.  I never actually clicked on the link.

I was looking through the other tabs and noted that even though the URL string in the Web Document had not changed, the URL string had changed under the Details tab.  I double clicked the link and it went to the correct webpage.

Apparently, the CopyFile is copying the new file over and changing the URL string under the Details tab but the string in the Web Documents URL section is not being changed.  This is absolutely odd.  I don't understand how this can happen when I delete the file before I do the CopyFile.

So the script works but with a strange anomaly that will confuse someone later.  If someone looks in the Web Document tab in the Properties for the link, what the see will not be where the link points to.
0
 
LVL 12

Expert Comment

by:jkaios
ID: 40367334
...I bet that the first DIR command (without quotes) will result in "The system cannot find the file specified."

while the second will will show the file, if exists, with its date/time, size, etc.
0
 
LVL 12

Expert Comment

by:jkaios
ID: 40367354
Please run the following commands from the Command Prompt and see which one works (note the difference - without quotes and with quotes):

1 - No quotes around the path:
  DIR C:\scripts\shortcut\Picture Perfect 4 Client.url

2 - Double quotes around the path
  DIR "C:\scripts\shortcut\Picture Perfect 4 Client.url"

If the second one works then it's the same as putting chr(34) before and after the filename variable that you're referencing in your script.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40367364
"DIR" is an internal native command, so yes it needs to be enclosed in quotes.
AFAIK the filesystemobject is not limited in the same regard...
0
 
LVL 12

Expert Comment

by:jkaios
ID: 40367383
AFAIK the filesystemobject is not limited in the same regard...

Thanks sirbounty for that clarafication...I've just confirmed. Yes, that's true!

My bad, please disregard my previous comments.
0
 

Author Comment

by:yccdadmins
ID: 40373329
Whoever can figure this out is a rock star.  To clarify what I've discovered is happening:

As stated earlier, the code is working but there is some type of file information caching going on that threw me.  The script copies the file now as intended after I fixed the file extension bit.

I was only visually checking the file properties of the link that was copied/renamed.  Notice that when I clicked on it, it went to the right webpage even though the URL string in the Web Document tab of the file properties was incorrect.  I check the Details tab and noted that the URL string there was correct.

I deleted the old file and copied the new file manually with the same results.  Only one field changed.  When you open the file with text editor, only the Details URL string is shown so the information in the Web Document tab is cached and being pulled from cache when you view the file after the copy/rename.

Here is the link Properties before copy:

Before-CopyRename.png
Here are the link properties after copy - note that the Web Document tab URL string does not change:

After-CopyRename.png
This is not a big deal for someone who knows the story but if someone were to come behind me to troubleshoot without properly reading my documentation they would check that first URL string and be pretty confused.  I have a theory that this is only occurring because I am copying a new link but using the same file name.  I think that if I change my script to use replace but use different file names, the caching factor will go away.

I'm going to try that today but if someone can figure out how to clear whatever cache is holding the extra file properties at the time of copy/replace you'll be the rock star for sure.
0
 

Author Comment

by:yccdadmins
ID: 40374087
That was it.  Changed my script a bit so that I'm not using the exact same file name for the new link and everything works now....manually at least.

1. So the first issue corrected was the use of the wrong file extension - .url instead of .lnk worked.

2. The script worked after that but another minor issue occurred - wrong information in Web Document tab URL string.  This is caused by they system caching file properties information - when renamed the file to the same file name as the file was being replaced the cached information was applied.  This was purely aesthetic since the URL string in the Details tab is what is actually read when the shortcut is clicked on.

3.  I could not find out how to remove cached information in time so to ensure that there will be no confusion regarding the cached URL, I changed the script to use a different file name for each shortcut - no cached file properties applied.

Unfortunately I have no use for these points.  I'm going to try and apply to all of you since you all had input on this.
0
 

Author Closing Comment

by:yccdadmins
ID: 40374127
Split the points since everyone worked on the solution which was simply using a different destination file name during copy/rename.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now