使用Powershell移动文件时如何执行一次记事本

时间:2018-06-27 02:58:42

标签: 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}

1 个答案:

答案 0 :(得分:0)

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

$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
}