[英]Have File Explorer Current working Directory Passed to PowerShell Script
Good Afternoon All!大家下午好!
Working on a script that my co-workers use, we move a lot up to Sharepoint and the objective was to make an easy method to set the file to read-only and compress it using the built-in NTFS compression.在处理我的同事使用的脚本时,我们将很多内容移至 Sharepoint,目的是创建一种简单的方法将文件设置为只读并使用内置的 NTFS 压缩对其进行压缩。
All of that is functioning like it should with the code below.所有这些都像下面的代码一样运行。 The problem I need to solve is this, somehow I need to pass the current directory the worker has open in File explorer to The open file dialog box.
我需要解决的问题是,我需要以某种方式将工作人员在文件资源管理器中打开的当前目录传递给打开文件对话框。 So that when the script is run you don't have to manually change it to the correct location.
因此,当脚本运行时,您不必手动将其更改为正确的位置。
Currently, I have it set to open on the users desktop directory目前,我已将其设置为在用户桌面目录中打开
Is this possible?这可能吗?
Thanks谢谢
Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
InitialDirectory = [Environment]::GetFolderPath('Desktop')
Multiselect = $true # Multiple files can be chosen
}
[void]$FileBrowser.ShowDialog()
$path = $FileBrowser.FileNames;
If($FileBrowser.FileNames -like "*\*") {
# Do something before work on individual files commences
$FileBrowser.FileNames #Lists selected files (optional)
foreach($file in Get-ChildItem $path){
Get-ChildItem ($file) |
ForEach-Object {
Set-ItemProperty -Path $path -Name IsReadOnly -Value $true
compact /C $_.FullName
}
}
# Do something when work on individual files is complete
}
else {
Write-Host "Cancelled by user"
}
The following solution determines the directory that the topmost File Explorer window has open (the window that is highest in the Z-order). 以下解决方案确定最上面的“文件资源管理器”窗口(Z顺序中最高的窗口)打开的目录。
That is, if File Explorer itself is active, that active window's directory is used, otherwise it is the most recently active window's. 也就是说,如果文件资源管理器本身是活动的,则使用该活动窗口的目录,否则,它是最近活动的窗口的目录。
# Helper type for getting windows by class name.
Add-Type -Namespace Util -Name WinApi -MemberDefinition @'
// Find a window by class name and optionally also title.
// The TOPMOST matching window (in terms of Z-order) is returned.
// IMPORTANT: To not search for a title, pass [NullString]::Value, not $null, to lpWindowName
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
'@
# Get the topmost File Explorer window, by its class name.
$hwndTopMostFileExplorer = [Util.WinApi]::FindWindow(
"CabinetWClass", # the window class of interest
[NullString]::Value # no window title to search for
)
if (-not $hwndTopMostFileExplorer) {
Write-Warning "There is no open File Explorer window."
# Alternatively, use a *default* directory in this case.
return
}
# Using a Shell.Application COM object, locate the window by its hWnd and query its location.
$fileExplorerWin = (New-Object -ComObject Shell.Application).Windows() |
Where-Object hwnd -eq $hwndTopMostFileExplorer
# This should normally not happen.
if (-not $fileExplorerWin) {
Write-Warning "The topmost File Explorer window, $hwndTopMostFileExplorer, must have just closed."
return
}
# Determine the window's active directory (folder) path.
$fileExplorerDir = $fileExplorerWin.Document.Folder.Self.Path
# Now you can use $fileExplorerDir to initialize the dialog:
Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
InitialDirectory = $fileExplorerDir
Multiselect = $true # Multiple files can be chosen
}
$null = $FileBrowser.ShowDialog()
# ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.