Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 每次将文件添加到文件夹时运行PS命令_Powershell_Powershell 2.0_Powershell 3.0 - Fatal编程技术网

Powershell 每次将文件添加到文件夹时运行PS命令

Powershell 每次将文件添加到文件夹时运行PS命令,powershell,powershell-2.0,powershell-3.0,Powershell,Powershell 2.0,Powershell 3.0,我试图创建一个脚本,它的主要任务是在每次将文件添加到文件夹时执行几个PS命令。 我已经使用“do”进行了绑定,它将以循环方式进入程序。但是,当我将文件添加到文件夹中时,命令只执行一次,然后新添加的文件就会出现错误。下面是我的代码示例 你知道我怎么做吗? 提前谢谢 do{ Dir *.txt | rename-item -newname { [io.path]::ChangeExtension($_.name, "eml")} Invoke-Item C:\Users\*.eml Remove-I

我试图创建一个脚本,它的主要任务是在每次将文件添加到文件夹时执行几个PS命令。 我已经使用“do”进行了绑定,它将以循环方式进入程序。但是,当我将文件添加到文件夹中时,命令只执行一次,然后新添加的文件就会出现错误。下面是我的代码示例 你知道我怎么做吗? 提前谢谢

do{
Dir *.txt | rename-item -newname { [io.path]::ChangeExtension($_.name, "eml")}
Invoke-Item C:\Users\*.eml
Remove-Item C:\Users\*.eml
} until ("condition that is never met")

您正在寻找一个
FileSystemWatcher
以及 要在添加文件时获得通知,请执行以下操作:

$folder = 'c:\test'
$filter = '*.txt'
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{ IncludeSubdirectories = $true}

$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
    $path = $Event.SourceEventArgs.FullPath
    # A file has been added, run your ps commands here. e. g.
    write-host $path
}
要注销事件,请使用:

Unregister-Event -SourceIdentifier FileCreated

您正在寻找一个
FileSystemWatcher
以及 要在添加文件时获得通知,请执行以下操作:

$folder = 'c:\test'
$filter = '*.txt'
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{ IncludeSubdirectories = $true}

$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
    $path = $Event.SourceEventArgs.FullPath
    # A file has been added, run your ps commands here. e. g.
    write-host $path
}
要注销事件,请使用:

Unregister-Event -SourceIdentifier FileCreated

更具体地说:“将文件添加到目录”是什么意思?你是说文件的数量会增加吗?或者文件总数不需要增加(只是将一个文件替换为另一个文件就是“添加”)?另外:更改文件名是否“添加”?更改文件内容是否“添加”?更具体地说:“将文件添加到目录”是什么意思?你是说文件的数量会增加吗?或者文件总数不需要增加(只是将一个文件替换为另一个文件就是“添加”)?另外:更改文件名是否“添加”?正在“添加”更改文件内容吗?