windows shell打开的Echo目录:startup

6jjcrrmo  于 2023-06-24  发布在  Windows
关注(0)|答案(1)|浏览(134)

在命令提示符下输入start shell:startup时,它会在启动文件夹中打开一个新窗口。我怎样才能打印路径到该文件夹,但实际上不打开文件夹在资源管理器?

nukf8bse

nukf8bse1#

可以使用.NET's Environment.SpecialFolder。此PowerShell命令将为您提供启动文件夹的路径

[Environment]::GetFolderPath([Environment+SpecialFolder]::Startup)

如果你真的想使用cmd,那么你可以这样调用上面的命令

powershell -C [Environment]::GetFolderPath([Environment+SpecialFolder]::Startup)

并使用use for /f将其保存到一个变量中
或者使用混合Batch/VBS解决方案

<!-- : Begin batch script
@echo off
cscript //nologo "%~f0?.wsf" %1
exit /b

----- Begin wsf script --->
<job><script language="VBScript">

Set oShell = CreateObject("Wscript.Shell")
Set oSFolders = oShell.SpecialFolders
WScript.Echo oSFolders("Startup")

</script></job>

或混合批次/Js

@if (@CodeSection == @Batch) @then
@echo off
cscript //e:jscript //nologo "%~f0" %*
exit /b
@end

// JScript Section
WScript.Echo((new ActiveXObject("shell.application")).namespace(0x07).Self.Path);

0x 07这里是Shell Special Folder Constant for startup
只需将上面的2个片段保存为 *.bat文件并运行。或者剥离批料部分并使其成为纯VBS/Js溶液。
事实上,这些代码段使用COM对象,因此它们可以用任何支持COM对象的语言编写,例如PowerShell:

(New-Object –ComObject Shell.Application).namespace(0x07).Self.Path

$c = New-Object -ComObject Wscript.Shell
$c.SpecialFolders("Startup")

使用纯批处理获得路径是可能的,而不求助于PowerShell、VBS或Js,但要棘手得多
注册表中包含特殊文件夹路径的2个键:HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell FoldersHKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders。下面是从this answer中解析它们的方法:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

set "StartupFolder="
for /F "skip=1 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Startup 2^>nul') do if /I "%%I" == "Startup" if not "%%~K" == "" if "%%J" == "REG_SZ" (set "StartupFolder=%%~K") else if "%%J" == "REG_EXPAND_SZ" call set "StartupFolder=%%~K"
if not defined StartupFolder for /F "skip=1 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v Startup 2^>nul') do if /I "%%I" == "Startup" if not "%%~K" == "" if "%%J" == "REG_SZ" (set "StartupFolder=%%~K") else if "%%J" == "REG_EXPAND_SZ" call set "StartupFolder=%%~K"
if not defined StartupFolder set "StartupFolder=\"
if "%StartupFolder:~-1%" == "\" set "StartupFolder=%StartupFolder:~0,-1%"
if not defined StartupFolder set "StartupFolder=%UserProfile%\Startup"

echo Startup folder is: "%StartupFolder%"

endlocal

相关问题