问题
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.
回答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
}
回答2:
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 $_ }
来源:https://stackoverflow.com/questions/58050529/need-windows-command-file-that-runs-all-commands-in-a-folder