# Solved: Copy shortcuts from network drive to desktop



## Gudzy (Jan 26, 2009)

I want to create a logon script that checks to see if users have all desktop icons that they should.

I've got the code below which works for one specified shortcut, but I want to recurse through a network folder and add any that aren't on the users desktop (it can't overwrite any that present as the users like to have there own custom icons)

If not exist "%userprofile%\Desktop\nameOfDatabase.accde.lnk" xcopy "\\network\location\nameOfDatabase.accde.lnk" "%userprofile%\Desktop\*" /Y /Q

Could someone point me in the right direction with a recursive script?

Thanks


----------



## TheOutcaste (Aug 8, 2007)

You have a "Master Folder" that contains a copy of all the Icons they should have, and want to copy any that don't exist, correct?

```
@Echo Off
Set _SrcFld=\\Server\Shortcuts
Set _DstFld=%Userprofile%\Desktop
For /F [COLOR=Red]"[/COLOR]Delims=[COLOR=Red]"[/COLOR] %%I In ([COLOR=Blue][B]'[/B][/COLOR]Dir /A-D /B [COLOR=Red]"[/COLOR]%_SrcFld%\*.lnk[COLOR=Red]"[/COLOR][COLOR=Blue][B]'[/B][/COLOR]) Do If Not Exist [COLOR=Red]"[/COLOR]%_DstFld%\%%I[COLOR=Red]"[/COLOR] Xcopy /CQY [COLOR=Red]"[/COLOR]%_SrcFld%\%%I[COLOR=Red]"[/COLOR] [COLOR=Red]"[/COLOR]%_DstFld%[COLOR=Red]"[/COLOR]>Nul
```
That should do it. Edit the *_SrcFld* path as needed.

Jerry


----------



## Gudzy (Jan 26, 2009)

TheOutcaste said:


> You have a "Master Folder" that contains a copy of all the Icons they should have, and want to copy any that don't exist, correct?


Thats exactly my problem.

When I run it I get "%%I was unexpected at this time"


----------



## TheOutcaste (Aug 8, 2007)

Are you saving it as a file and running it, or typing/copying it directly into a command prompt?
If typing at the prompt, you have to change all doubled percent symbols to single (%% to %)


----------



## Gudzy (Jan 26, 2009)

Ah Ok, I was copying and pasting part of it so that solves that.

I now get a "the system cannot find the file specified" error. Also the command prompt box only flashes on screen and then disappears, how can you make it stay on screen?


----------



## TheOutcaste (Aug 8, 2007)

For testing probably easiest to open a command prompt first. Then it will stay open.

Or you can add a Pause statement at the end. The window will stay open until you hit a key.

A mis-typed path for the server, a missing quote, or if the server is offline would cause the file not found error.

The Set _SrcFld entry should be just *\\Servername\Sharename\pathtofolder*
pathtofolder is needed only if the actual shared folder is a parent of the folder with the shortcuts

It should not include any shortcut names.

If you remark out the Echo Off (REM @Echo Off) that might help you spot what it is as you'll see each file name it's looking for.


----------



## Gudzy (Jan 26, 2009)

I've set the _srcFld to the folder where the icons are, if I type the value of _srcFld into windows explorer it shows all the files I want to copy.

Could you explain the red highlighted pathtofolder part a bit more, I don't really understand (unless the above confirms thats fine)

I put in a pause statement at the end, it doesn't show any file names before showing the error.

Edit: 
It displays this for the last line of code:

C:\Documents and Settings\User\Desktop>For /F "Delims=" %I In <'Dir /A-D /B ""\\myservername\path1\path2\Desktop\Desktop Shortcut Icons"\*.lnk"'> Do If Not Exist "C:\Documents and Settings\User\Desktop\%I" Xcopy /CQY ""\\myservername\path1\path2\Desktop\Desktop Shortcut Icons"\"%I "C:\Documents and Settings\User\Desktop" 1>Nul
The system cannot find the file specified

There seems to be a difference with some paths having "pathname\%I" and some having ""pathname"\"%I
Could this be the problem?


----------



## TheOutcaste (Aug 8, 2007)

Gudzy said:


> I've set the _srcFld to the folder where the icons are, if I type the value of _srcFld into windows explorer it shows all the files I want to copy.


That should be good then.


Gudzy said:


> I put in a pause statement at the end, it doesn't show any file names before showing the error.


Did you remark out the @echo Off line?
Change it to read REM @echo Off
Be sure there is a space between REM and @

With Echo not turned off, you should get this:
The two set statements, then the entire For statement, then you'll get a separate line for each shortcut showing just the If Not Exist part. I've shown just one:

```
[COLOR=DimGray]C:\test1>[/COLOR]Set _SrcFld=\\MrBoulder\Users\TheOutcaste\Desktop

[COLOR=DimGray]C:\test1>[/COLOR]Set _DstFld=C:\Documents and Settings\TheOutcaste\Desktop

[COLOR=DimGray]C:\test1>[/COLOR]For /F "Delims=" %I In ('Dir /A-D /B "\\MrBoulder\Users\TheOutcaste\Desktop\*.lnk"') Do If Not Exist "C:\Documents and Settings\TheOutcaste\Desktop\%I" Xcopy /CQY"\\MrBoulder\Users\TheOutcaste\Desktop\%I" "C:\Documents and Settings\TheOutcaste\Desktop\" 1>Nul

[COLOR=DimGray]C:\test1>[/COLOR]If Not Exist "C:\Documents and Settings\TheOutcaste\Desktop\Component Services.lnk" Xcopy /CQY "\\MrBoulder\Users\TheOutcaste\Desktop\Component Services.lnk" "C:\Documents and Settings\TheOutcaste\Desktop\" 1>Nul
```
I've edited the post with the code to color code the double quotes and single quotes.

Can't think of what it could be other than a typo someplace. Make sure you didn't get an extra space someplace in the *Set _SrcFld* line. There can't be one after the equals sign, or after the path.

'Fraid it's time for me to run hit the sack for a while, hope this give you enough clues to figure it out.

Jerry


----------



## TheOutcaste (Aug 8, 2007)

Gudzy said:


> "*"*\\myservername\path1\path2\Desktop\Desktop Shortcut Icons*"*\*.lnk"'
> 
> "*"*\\myservername\path1\path2\Desktop\Desktop Shortcut Icons*"*\"%I
> 
> ...


Didn't see this before I hit send. That looks like the problem.
Did you put quotes on the Set _SrcFld line? That should not have any quotes, should read just like this:
*Set _SrcFld=\\myservername\path1\path2\Desktop\Desktop Shortcut Icons*

Jerry


----------



## Gudzy (Jan 26, 2009)

Taken the quotes off around the path now and that fixes that error 

Edit:
I'll have a play now, think I should be able to sort it and you can go to bed!

Thanks for the help so far!

Second Edit:
Now works as expected. Many thanks.


----------



## TheOutcaste (Aug 8, 2007)

You're Welcome.

Al least you know that a path with spaces needs quotes. You can quote it in the Set statement, or put the quotes around the variable each time it's used. Just can't always mix the two methods.

Glad to help, and I'm off.


----------

