需要Windows命令文件来运行文件夹中的所有命令 [英] Need Windows command file that runs all commands in a folder

查看:37
本文介绍了需要Windows命令文件来运行文件夹中的所有命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://www.online-tech-tips.com/computer-tips/create-windows-batch-files/解释了如何创建打开一组特定Windows命令的命令(批处理)文件.我想对此进行概括,所以我可以创建包含命令快捷方式的文件夹并运行该文件夹(使用命令文件),这意味着我可以使用命令文件执行该文件夹中包含的所有命令快捷方式.

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.

我认为我所需要的只是一种循环扫描文件夹并执行文件夹中每个命令的方法.可能是Windows标准的.cmd文件(由cmd.exe运行)可以执行此操作,但如果没有,则可以使用Powershell(由.ps1命令文件使用).

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.

推荐答案

Walter Mitty的有用答案显示了一个PowerShell命令,该命令可以使用 Start-Process 打开当前文件夹中的所有快捷方式文件( *.lnk ).

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

以下代码将其合并到名为 Open Shortcuts 的快捷菜单命令定义中,该定义将可用:

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.

请注意,我的目标是 HKEY_CURRENT_USER \ Software \ Classes ,而不是 HKEY_CLASSES_ROOT ,这使定义特定于用户,并且也不需要在高程下运行:

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
}

这篇关于需要Windows命令文件来运行文件夹中的所有命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆