简体   繁体   English

批处理脚本+多个变量

[英]Batch Script + Multiple variables

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 它从某个文件夹中获取所有文件,并将每个文件放入rar

  • it generates an URL shortcut for the desktop 它为桌面生成URL快捷方式

So far everything works fine, but here is the problem: I would like to create the shortcuts with unique URLs. 到目前为止,一切都工作正常,但这是问题所在:我想创建具有唯一URL的快捷方式。 Right now I am simply passing the URL via the script like this: 现在,我只是通过这样的脚本传递URL:

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 . 当然,通过这种方式,所有快捷方式都将指向http://example.com

I would like to be able to define an unique URL for each shortcut that is being created. 我希望能够为正在创建的每个快捷方式定义一个唯一的URL。

Since I am a total beginner, I dont really know where to start... and was hoping for a suggestion! 由于我是一个初学者,所以我真的不知道从哪里开始……并希望提出建议!

PS Just to give you an idea what I would need, but this doesnt work: PS:只是为了给您一个想法,我需要什么,但这是行不通的:

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. 然后脚本应将URL 1用于第一个快捷方式,将URL 2用于第二个快捷方式,依此类推。

Update: 更新:

It works fine now following jeb´s answer below. 现在,按照下面jeb的回答,它可以正常工作。 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: 并且NewVar应该保存为:

SET NEWVAR=%2

How could I make the script know when the URL variables are done and the NewVar variables start? 当URL变量完成并且NewVar变量启动时,如何使脚本知道?

And than be called like 而不是像

!NEWVAR!

You should avoid spaces when using SET else you create variable names with spaces! 使用SET时,应避免使用空格,否则将使用空格创建变量名!

You can enumerate all parameters with the SHIFT command. 您可以使用SHIFT命令枚举所有参数。

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. SHIFT将移动所有参数,因此,下次访问%1时,将获得下一个参数。
But as you are in a block you can't access the current %1 directly, so I use the CALL %% trick here. 但是由于您处在一个区块中,因此无法直接访问当前的 %1,因此我在这里使用CALL %%技巧。

And to access the current value of LINK in the block I used the delayed expansion ( !Link! ). 为了访问块中LINK的当前值,我使用了延迟扩展( !Link! )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM