如何使用powershell移动文件时附加增量文件名并执行记事本 [英] How to append incremental filename and execute notepad when the file is being moved using powershell
问题描述
您好我是PowerShell的新手
目前我正在尝试在powershell上编写脚本。
1.监控特定文件夹中的更改(C:\ Users \CELINE \Desktop \ TestT4)
2.如果有新的检测到文件后,它会自动将文件移动到另一个文件夹(C:\Users \CELINE \Desktop \ PowerShellChanges2)
3.自动重命名移动的文件文件到Request.csv
4.将新文件移动到文件夹时,将增量编号附加到文件名(例如Request(1).csv ,请求(2).csv,请求(3).csv等)
5.文件移动后打开记事本
我现在卡在第4点和第5点。
我不知道为什么文件被移动时,多个记事本连续打开但我只想打开一个
我也不知道如何追加文件名
一个能帮忙吗?
我尝试过:
$ watcher = New-Object System.IO.FileSystemWatcher
#要观看的文件夹的路径
$ watcher .Path =C:\ Users\CELINE \Desktop\Testing4
$ watcher.Filter =*。*
$ watcher.IncludeSubdirectories = $ true
$ watcher.EnableRaisingEvents = $ true
#检测到变化时执行的行动
$ action = {
#SourcePath
$ path = $ Event.SourceEventArgs .FullPath
$ changeType = $ Event.SourceEventArgs.ChangeType
$ logline =$(Get-Date),$ changeType,$路径
#目标路径
$ dest =C:\ Users \CELINE \Desktop\PowerShellChanges2 \ Request .csv
#添加日志文件来监控更改
添加内容C:\\ \\Users\C ELINE\Desktop\logtest5.txt-value $ logline
#Move Files
$ actionMove = Move-Item -Path $ path -Destination $ dest
#Execute Notepad
$ FileExists = Test-Path $ dest
If($ FileExists -eq $ True){C:\ Windows\\\
otepad.exe }
}
#Event Trigger
Register-ObjectEvent $ watcher已更改-Action $ action
while($ true){Sleep 0}
Hi I am new to powershell
Currently I am trying to write a script on powershell.
1. Monitor the Changes in a particular folder (C:\Users\CELINE\Desktop\Testing4)
2. If there is a new file detected, it will automatically move the file to another folder (C:\Users\CELINE\Desktop\PowerShellChanges2)
3. Automatically rename the moved file to "Request.csv"
4. Append incremental numbers to the file names when new files is being moved to the folder (eg. Request(1).csv, Request(2).csv, Request(3).csv etc)
5. Open a notepad after the file is moved
I am now stuck with point 4 and 5.
I have no idea why when the file is being moved, multiple notepads are opened continuously but I only want to open one
I also do not know how to append the file names
Anyone able to help?
What I have tried:
$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
#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}
推荐答案
watcher = New-Object System.IO.FileSystemWatcher
#要观看的文件夹路径
watcher = New-Object System.IO.FileSystemWatcher
#Path of folder to watch
watcher.Path =C:\ Users \ CELINE \Desktop\Testing4
watcher.Path = "C:\Users\CELINE\Desktop\Testing4"
watcher.Filter =*。*
watcher.Filter = "*.*"
这篇关于如何使用powershell移动文件时附加增量文件名并执行记事本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!