0

https://www.online-tech-tips.com/computer-tips/create-windows-batch-files/ explains how to create a command (batch) file that opens a set of specific Windows commands. I'd like to generalize this, so I can create folders that contain command shortcuts and run such a folder (using a command file), meaning that I can execute all the command shortcuts contained in the folder using the command file.

I've searched the Web and can't find such a command file.

I think all I need is a way to scan the folder and execute each command in the folder, in a loop. Probably a Windows standard .cmd file (run by cmd.exe) could do this, but if not, the Powershell could be used (by a .ps1 command file).

An example would be to create a folder on the desktop containing several command shortcuts related to some specific and repetitive processing (say, making a movie, or building an application). I could edit these commands simply by opening the folder in Explorer. When I want to run all the commands, each in its own window, all I would have to do is right-click the folder on the desktop and select the name of the command file that runs all the shortcuts in the folder.

I hope this is clear and that it is clear why such a command file would be very useful to use when returning to a project after having worked on other projects.

If not, just ask questions in the comments.

mklement0
  • 382,024
  • 64
  • 607
  • 775
David Spector
  • 1,520
  • 15
  • 21
  • powershell allows you to call other scripts into a main script. you could save several scripts into files and the `dot-source` them into your main file that would then run the code. ///// i confess that your goal makes no sense to me. [*blush*] why not just write the script that does what you want without the horrifying risk that your code would run something that you didn't intend to run? – Lee_Dailey Sep 22 '19 at 15:27
  • 1
    Too bad, too broad. Loads of questions and no code provided. – Gerhard Sep 22 '19 at 16:03
  • 1
    Sorry, I had no idea how to start writing such a command file. The goal should make good sense to anyone familiar with Windows, as the Windows startup files already work this way (they are automatically executed by Windows during startup, is the only difference). See the accepted answer for what I was asking for. – David Spector Sep 22 '19 at 21:15

2 Answers2

3

I'm confused by the phrase "command shortcuts". If all you want to do is find all the shortcut files in the current directory, and start each one in a separate window, this is all you need.

gci *.lnk | % { start $_ }
Walter Mitty
  • 18,205
  • 2
  • 28
  • 58
  • This appears to work perfectly. I will have to figure out how to get it to work with a right-click on the folder, but this should not be too difficult. I hope that others benefit from this code, too, and thanks. – David Spector Sep 22 '19 at 21:13
  • Okay. I've added a command to the context menu for folders as follows: (1) added new Registry key HKEY_CLASSES_ROOT\Folder\shell\runlinks with default value "runlinks"; (2) added new key HKEY_CLASSES_ROOT\Folder\shell\runlinks\command with default value "C:\ProgFile\Runlinks.ps1" "%1" . When I test this by right-clicking a test folder and selecting "runlinks" from the context menu, I get a bright blue dialog box containing the message "This app can't run on your PC." I'm stuck at this point. – David Spector Sep 22 '19 at 22:18
  • As you've decided to create the context menu for all profiles, not the Current_User, perhaps you've not set the ExecutionPolicy appropriately for everyone to use. – Compo Sep 22 '19 at 22:36
  • I will also ask the purpose! I cannot see much benefit in right-clicking on a folder and left clicking a menu item as opposed to double-click that folder and double-click on a script. – Compo Sep 22 '19 at 22:47
  • 1
    I will try to move my two keys to CU instead of LM. I didn't realize this could be a problem. As to the purpose, I'd like to have several folders containing links relevant to specific tasks (any number of links), and execute all those links (shortcuts) at once. Double-clicking many icons is an operation suitable to computer automation. – David Spector Sep 22 '19 at 22:51
  • @DavidSpector, my comment was specific, you'd only double-click the folder then double-click the script. Speed wise I see little difference in right-click once on the folder, and left click once on the context menu item. – Compo Sep 23 '19 at 14:43
  • I think "This app can't run on your PC" is caused by the "start" command. I say this because relaxing the powershell execution policy didn't help. But when I tried to Bypass the policy for the file, I got an error message (see next comment). – David Spector Sep 23 '19 at 16:29
  • Error message: PS C:\WINDOWS\system32> PowerShell.exe -ExecutionPolicy Bypass -File C:\ProgFile\Runlinks.ps1 start : This command cannot be run due to the error: The operation was canceled by the user. At C:\ProgFile\Runlinks.ps1:1 char:17 + gci *.lnk | % { start $_ } + ~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand – David Spector Sep 23 '19 at 16:29
1

Walter Mitty's helpful answer shows a PowerShell command that opens all shortcut files (*.lnk) in the current folder, using Start-Process.

Here is code that incorporates it into a shortcut-menu command definition named Open Shortcuts, which will become available:

  • when you right-click on a folder on the Desktop or in File Explorer

  • when you right-click on the open folder's background in File Explorer (in which case the command will apply to that open folder).

If shortcut files are present in a given folder, they are all opened (asynchronously), as if they had been double-clicked; in the absence of shortcuts, a warning is displayed.

Note that I'm targeting HKEY_CURRENT_USER\Software\Classes rather than HKEY_CLASSES_ROOT, which makes the definition user-specific and also doesn't require running with elevation:

# Define a shortcut-menu command that opens all shortcut files (*.lnk) in the target folder (%V):

# Define the name to appear in the shortcut menu.
$commandName = 'Open Shortcuts' 

# Define the PowerShell command to run, hidden, via mshta.exe, so that no PowerShell console window opens (temporarily).
$command = @"
mshta.exe vbscript:(CreateObject("WScript.Shell").Run("powershell.exe -noexit -noprofile -c `$f = Get-Item \""%V\*.lnk\""; if (`$f) { `$f | ForEach-Object { Start-Process `$_.FullName } } else { (New-Object -ComObject WScript.Shell).Popup(\""%V contains no shortcut files (*.lnk).\"",0,\""$commandName\"",48) }",0))(Window.Close)'
"@

# Define the shortcut-menu commands in the registry, for:
#  * folders
#  * the background of open folders (to apply the command to the open folder)
'Folder', 'Directory\Background' | ForEach-Object {
  New-Item -Force "HKCU:\Software\Classes\$_\shell\$commandName\command" |
    Set-ItemProperty -Name '(Default)' -Value $command
}
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    Yes, this worked! I am very happy. I named the command "Run All Links" and when I click it in a folder context menu, all the links are run without unexpected error messages. Perfect! Should be of interest to all "power users" of Windows. – David Spector Sep 23 '19 at 16:44
  • Glad to hear it, @DavidSpector; yes, it sounds like a handy feature. – mklement0 Sep 23 '19 at 17:14