How to create links in automatic?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
LiveITA
Posts: 12
Joined: 25 Jan 2018 14:22

How to create links in automatic?

#1 Post by LiveITA » 11 Mar 2018 19:20

Post results: to change a preexisting link use:

Code: Select all

Set Shell = CreateObject("WScript.Shell")
Set link = Shell.CreateShortcut("pathto\linktomodify.lnk")
link.IconLocation = "pathto\icons.dll,"& WScript.Arguments(1)
link.Arguments = WScript.Arguments(0)
link.Save
Be sure to use the & for the icons!!! if you don't the parameter won't be read
This will change the argument to the 2nd parameter and the icon to the 1st parameter
calli it like this in a batch file or via cmd:

Code: Select all

scriptname.vbs "1st parameter" "2nd parameter" "more parameters up to 9"
Explanation:

Code: Select all

Set link
is the existing link

Code: Select all

link.IconLocation
is the dll containing the icon you want to use

Code: Select all

link.Arguments
is the parameter you want to output

Automatic creation is possible but can't supply all the parameters

use

Code: Select all

Set Shell = CreateObject("WScript.Shell")
DesktopPath = Shell.SpecialFolders("Desktop")
Set link = Shell.CreateShortcut(DesktopPath & "\winipcfg.lnk")
link.Arguments = ""
link.Description = "winipcfg shortcut"
link.HotKey = "CTRL+ALT+SHIFT+X"
link.IconLocation = ",0"
link.TargetPath = "c:\windows\winipcfg.exe"
link.WindowStyle = 1
link.WorkingDirectory = "d:\test"
link.Save
in a *.vbs to make it work
You can use WScript.Arguments(0), WScript.Arguments(1), WScript.Arguments(2) to add more parameters for a faster call via another batch script

For automatic creation use:
Last edited by LiveITA on 12 Mar 2018 13:35, edited 7 times in total.

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: How to create links in automatic?

#2 Post by elzooilogico » 12 Mar 2018 04:48

as a starting point here is a script that uses vbs to create links. you only need to tell createShortut the real data

Code: Select all

@ echo off
SetLocal EnableDelayedExpansion EnableExtensions

set "target="                                 & rem put here your real file (if empty this script will be used)
set "description="                            & rem put link description here (if empty 'Link to filename' will be used)
set "dll=%SystemRoot%\System32\imageres.dll"  & rem icon container
set/A linkIconNumber=28	                      & rem icon number (net drive)

call:createShortCut "%target%", "%description%", "%dll%", "%linkIconNumber%"
call:createShortCut "%SystemRoot%\notepad.exe", "%description%", "%SystemRoot%\notepad.exe", "0"

rem if running from explorer, wait some seconds
(((echo.%cmdcmdline%)|find /I "%~0")>NUL) && (timeout /T 7&echo/)
EndLocal
exit/B 1

:createShortCut target, description, iconDll, iconNumber
SetLocal EnableDelayedExpansion

rem set "HOME=%PUBLIC%"
set "HOME=%USERPROFILE%"

set "lnkTarget=%~1" & set "lnkDescription=%~2"

if NOT defined lnkTarget set "lnkTarget=%~dpnx0"
for %%i in (%lnkTarget%) do (set "lnkWorkDir=%%~dpi" & set "lnkFile=%HOME%\Desktop\Link to %%~ni.lnk")
if NOT defined lnkDescription for %%i in (%lnkTarget%) do set "lnkDescription=Link to %%~nxi"

set "_vbs_file_=%TEMP%\link.vbs"
if exist "%lnkFile%" del /F /Q "%lnkFile%" 2>NUL
>"%_vbs_file_%" (
  echo(Set oWS ^= wScript.CreateObject^("wScript.Shell"^)
  echo(linkFile ^= "%lnkFile%"
  echo(Set oLink ^= oWS.CreateShortcut^(linkFile^)
  echo(oLink.TargetPath ^= "%lnkTarget%"
  echo(oLink.WindowStyle ^= "1"
  echo(oLink.Description ^= "%lnkDescription%"
  echo(oLink.IconLocation ^= "%~3, %~4"
  echo(oLink.WorkingDirectory ^= "%lnkWorkDir%"
  echo(oLink.Save
)
if exist "%TEMP%\link.vbs" (set "_spawn_=%TEMP%\link.vbs") else (set "_spawn_=link.vbs")
start /MIN /WAIT cmd /C "cls & "%_spawn_%" %* & del /F /Q "%_spawn_%" 2>NUL"
rem remove this line
echo/Created %lnkFile% 
EndLocal
exit/B

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: How to create links in automatic?

#3 Post by elzooilogico » 12 Mar 2018 07:33

the new link will overwrite the existing one (if you have enough permissions) see http://www.tek-tips.com/viewthread.cfm?qid=850335

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: How to create links in automatic?

#4 Post by npocmaka_ » 12 Mar 2018 10:50

check this : https://github.com/npocmaka/batch.scrip ... tcutJS.bat
I hope the help message is descriptive enough.Besides the standard functionality the linked tool also can turn the 'Run As Administrator' on and off.

LiveITA
Posts: 12
Joined: 25 Jan 2018 14:22

Batch reitaration for multiple variables

#5 Post by LiveITA » 12 Mar 2018 12:21

I'm having some issues with my script. This script goal is to create a list of pdf and doc files of any USB and to create a link to each one on the desktop

Code: Select all

    :start
    for /f "skip=2 delims=" %%i in (
      '2^>nul wmic logicaldisk get caption^,description^,drivetype /format:csv'
    ) do for /f "tokens=2-4 delims=," %%j in ("%%i") do (
      if %%l==2 (
    for /f %%z in ('dir /b %%j*.doc %%j*.pdf') do set filelist=%%z
      )
    )
    goto start
This is the code that creates a list of the files located in each USB, and it works (partaily, it outputs only the last item) but: how can i output each file and each disk letter as variables couples?

For example i have 2 USB and 3 files

D:\onelo.pdf

D:\twobr.doc

E:\oneag.doc

I need to ouput each couple of %diskvar% (the USB drive letter) and %namevar% (the filename without the extension) to this script, plus a 3rd variable %numvar% which represents the file format converted to a number (pdf=1, doc=2)

As example for code iterations:

1st iteration should be diskvar= D:\ ; namevar=onelo ; numvar=1

2nd iteration should be diskvar= D:\ ; namevar=twobr ; numvar=2

3rd iteration should be diskvar= E:\ ; namevar=oneag ; numvar=2

All those iterations will be run by those batch commands:

Code: Select all

    copy "%diskvar%\link.lnk" "%diskvar%\linktomodify.lnk"
    linkedit.vbs %namevar% %numvar%
    ren "%diskvar%\linktomodify.lnk" "%namevar%.lnk"
    copy "%diskvar%\%namevar%.lnk" "desktoppath\%namevar%.lnk"
    del "%diskvar%\%namevar%.lnk"
My issue is being able to correlate these 2 batch part with some loop that execute the 2nd half for each file

LiveITA
Posts: 12
Joined: 25 Jan 2018 14:22

Re: How to create links in automatic?

#6 Post by LiveITA » 12 Mar 2018 13:18

Squashman wrote:
12 Mar 2018 12:35
WHAT ARE YOU DOING! Why did you remove all the comments in each of your posts in this thread. Now the thread makes absolutely no sense.
Answers were uncomplete so i just changed the main post. There was no full answer, bu thanks to the partial i got to the full answer. Ain't better to find the answer on the 1st post?

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: How to create links in automatic?

#7 Post by Squashman » 12 Mar 2018 13:26

Merged topics and deleted empty posts.

LiveITA
Posts: 12
Joined: 25 Jan 2018 14:22

Re: How to create links in automatic?

#8 Post by LiveITA » 12 Mar 2018 13:33

Squashman wrote:
12 Mar 2018 13:26
Merged topics and deleted empty posts.
They should not be merged, they cover 2 diffrent arguments: one is about a vbs script, the other is about batch for loops reiteration

LiveITA
Posts: 12
Joined: 25 Jan 2018 14:22

Re: How to create links in automatic?

#9 Post by LiveITA » 12 Mar 2018 19:41

Solved:
All the code in the for loop i tried to add always breaks so i made it like this:

After the for loop exit the loop with the current variables:

Code: Select all

goto nextpart
  )
)

:nextpart
then run the code that you need to run and be sure to invalidate the item you were working with. This way, once the loop starts again (add

Code: Select all

goto beginning
at the end and

Code: Select all

:beginning 
before anything else) it will just ignore the previuos managed file and go to the next.

Post Reply