0

I am currently trying to write my first batch script ever.

What it does:

  • it gets all files from a certain folder and puts each of it in a rar

  • it generates an URL shortcut for the desktop

So far everything works fine, but here is the problem: I would like to create the shortcuts with unique URLs. Right now I am simply passing the URL via the script like this:

script.bat http://example.com

And the script does the following:

SET LINK        =%1

::For each file that is in the dir "Ori"
FOR %%i IN (%BASEPATH%\Ori\*.*) DO (

    echo [InternetShortcut] > "%BASEPATH%\New\%%~ni.URL"
    echo URL=%LINK% >> "%BASEPATH%\New\%%~ni.URL"
    echo IconFile=%LINK%/favicon.ico >> "%BASEPATH%\New\%%~ni.URL"
    echo IconIndex=0 >> "%BASEPATH%\New\%%~ni.URL"

)

Of course this way, all Shortcuts will lead to http://example.com.

I would like to be able to define an unique URL for each shortcut that is being created.

Since I am a total beginner, I dont really know where to start... and was hoping for a suggestion!

P.S. Just to give you an idea what I would need, but this doesnt work:

script.bat {http://example.com,http://example1.com,http://example2.com}

And than the script should use URL 1 for the first shortcut, URL 2 for the second and so on.

Update:

It works fine now following jeb´s answer below. However, I was wondering: would it be possible to do the same job again with another variable? So that it looks kinda like this:

script.bat http://example.com http://example1.com http://example2.com NewVar1 NewVar2 NewVar3

And the NewVar should be saved just like:

SET NEWVAR=%2

How could I make the script know when the URL variables are done and the NewVar variables start?

And than be called like

!NEWVAR!

1 Answer 1

3

You should avoid spaces when using SET else you create variable names with spaces!

You can enumerate all parameters with the SHIFT command.

Then you can use something like

script.bat http://example.com http://example1.com http://example2.com

setlocal EnableDelayedExpansion
SET LINK=%1

::For each file that is in the dir "Ori"
FOR %%i IN (%BASEPATH%\Ori\*.*) DO (

    echo [InternetShortcut] > "%BASEPATH%\New\%%~ni.URL"
    echo URL=!LINK! >> "%BASEPATH%\New\%%~ni.URL"
    echo IconFile=!LINK!/favicon.ico >> "%BASEPATH%\New\%%~ni.URL"
    echo IconIndex=0 >> "%BASEPATH%\New\%%~ni.URL"
    shift
    call set link=%%1

)

The SHIFT will move all parameters, so the next time you access %1 you get the next parameter.
But as you are in a block you can't access the current %1 directly, so I use the CALL %% trick here.

And to access the current value of LINK in the block I used the delayed expansion (!Link!).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the quick answer! It works as expected now :) However, I was wondering if you might could even take a look at my updated question too?
»avoid« is too soft a word, I guess ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.