Frank Frank - 5 months ago 13
Bash Question

How to modify a .cmd file to loop through items?

I found a .cmd file online to do what I want, but it only does one item at a time, so I have to create multiple files to do each item.

It looks like this :

@echo off
set ITEM=
set TASKBAR=
set USAGE=

set ITEM=/item:"%windir%\System32\calc.exe"
set TASKBAR=/taskbar
:: set USAGE=/?

echo on
:: Pin to Taskbar
cscript //nologo PinItem.wsf %ITEM% %TASKBAR% %USAGE%


This will pin the calculator to Windows taskbar, but if I want to also pin notepad, IE, MS Paint ... to the task bar, I'll have to modify the script and save multiple copies to do different items.

So my question is : how to improve the script to loop through multiple items, something like :

set ITEM={ /item:"%windir%\System32\calc.exe" ,
/item:"%windir%\System32\notepad.exe" ,
/item:"%windir%\System32\mspaint.exe" }


Then do a for .. next loop over the %ITEM% ?

I don't know this script language, I guess there must be a way, what's the correct syntax ?

Answer

Please note that cscript command line is merely displayed for debugging purposes; remove ECHO to make it operational, no sooner than debugged.

@echo off
set "ITEM="
set "TASKBAR="
set "USAGE="

    rem make sure that there is no blank space after the caret
    rem                                     here ↓  
set ITEM=/item:"%windir%\System32\calc.exe"    ,^
         /item:"%windir%\System32\notepad.exe" ,^
         /item:"%windir%\System32\mspaint.exe"    

set TASKBAR=/taskbar
:: set USAGE=/?

rem echo on
:: Pin to Taskbar
for %%G in (%ITEM%) do (
    ECHO cscript //nologo PinItem.wsf %%G %TASKBAR% %USAGE%
)

Another approach, maybe more intuitive:

@echo off
SETLOCAL EnableExtensions
rem ↓     ↓  note double quotes
set "ITEM="
set "ITEM=%ITEM%, "%windir%\System32\calc.exe""
set "ITEM=%ITEM%, "%windir%\System32\notepad.exe""
set "ITEM=%ITEM%, "%windir%\System32\mspaint.exe""
rem ↑             ↑                             ↑↑ note double quotes

rem echo on
:: Pin to Taskbar
for %%G in (%ITEM%) do (
    ECHO cscript //nologo PinItem.wsf /item:"%%~G" /taskbar
)

Resources (required reading):

Comments