首页 > 解决方案 > 使用powershell移动文件时如何执行记事本

问题描述

嗨,我是 powershell 新手

目前我正在尝试在 powershell 上编写一个脚本。

  1. 监视特定文件夹中的更改 (C:\Users\CELINE\Desktop\Testing4)

  2. 如果检测到新文件,它会自动将文件移动到另一个文件夹(C:\Users\CELINE\Desktop\PowerShellChanges2)

  3. 自动将移动的文件重命名为“Request.csv”

  4. 将新文件移动到文件夹时,在文件名上附加递增数字(例如,Request(1).csv、Request(2).csv、Request(3).csv 等)

  5. 移动文件后打开记事本

我现在坚持第 5 点。我不知道为什么在移动文件时,会连续打开多个记事本,但我只想打开一个空记事本

有谁能帮忙吗?

这是我的代码

$watcher = New-Object System.IO.FileSystemWatcher

#Path of folder to watch

$watcher.Path = "C:\Users\CELINE\Desktop\Testing4"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true 

#Action executed when changes detected

$action = { 


#SourcePath
$path = $Event.SourceEventArgs.FullPath

$changeType = $Event.SourceEventArgs.ChangeType
$logline = "$(Get-Date), $changeType, $path"

#Destination path
$dest = "C:\Users\CELINE\Desktop\PowerShellChanges2\Request.csv"

#Add Log file to monitor the changes 

Add-content "C:\Users\CELINE\Desktop\logtest5.txt" -value $logline

#Move Files

$actionMove = Move-Item -Path $path -Destination $dest

#Append number to filename

If (Test-Path $dest) {
    $i = 0
    While (Test-Path $dest) {
        $i += 1
        $renamefile "C:\Users\CELINE\Desktop\PowerShellChanges2\Request($i).csv"
        Rename-Item -Path $dest -NewName $renamefile
    }
}





#Execute Notepad


$FileExists = Test-Path $dest
If ($FileExists -eq $True) {C:\Windows\notepad.exe}
} 

#Event Trigger
Register-ObjectEvent $watcher "Changed" -Action $action
while ($true) {Sleep 0}

标签: powershell

解决方案


您可以检查记事本是否已经在运行,如果没有,则启动一个新实例。例如,更改脚本的这一部分:

$FileExists = Test-Path $dest
If ($FileExists -eq $True) {C:\Windows\notepad.exe}
} 

类似于:

if ((Test-Path $dest) -and ((Get-Process | Where Id -eq $procId) -eq $null))
{
    $procId = (Start-Process -FilePath "C:\Windows\notepad.exe" -PassThru).Id
}

这仅检查您之前从脚本启动的记事本的特定实例,但忽略通过其他方式启动的任何副本。您可以轻松更改它以查找记事本的任何实例。

即使没有进行此更改,我的代码也存在一些问题,导致它停止工作,因此我对其进行了微调。下面的代码应该可以正常工作:

$watcher = New-Object System.IO.FileSystemWatcher

# Path of folder to watch
$watcher.Path = "C:\Users\CELINE\Desktop\Testing4"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true 

$procId = -1
$i = 1

# Action executed when changes detected
$action = 
{ 
    # SourcePath
    $path = $Event.SourceEventArgs.FullPath

    #Destination path
    $targetFile = "C:\Users\CELINE\Desktop\PowerShellChanges2\Request.csv"

    # Add Log file to monitor the changes
    Add-content -Path "C:\Users\CELINE\Desktop\logtest5.txt" `
                -value "$(Get-Date), $($Event.SourceEventArgs.ChangeType), $path"

    # Move file
    Move-Item -Path $path -Destination $targetFile

    # Rename file
    If (Test-Path $targetFile) 
    {
        $renamefile = "C:\Users\CELINE\Desktop\PowerShellChanges2\Request($i).csv"
        Rename-Item -Path $targetFile -NewName $renamefile
    }

    # Execute Notepad
    if ((Test-Path $renamefile) -and ((Get-Process | Where Id -eq $procId) -eq $null))
    {
        $procId = (Start-Process -FilePath "C:\Windows\notepad.exe" -PassThru).Id
    }

    $i++
}


# Register event
Register-ObjectEvent $watcher "Changed" -Action $action | Out-Null

# Wait
while ($true)
{
    Sleep 0
}

推荐阅读