Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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中每个文件夹的特定子文件夹_Powershell_Wildcard - Fatal编程技术网

扫描powershell中每个文件夹的特定子文件夹

扫描powershell中每个文件夹的特定子文件夹,powershell,wildcard,Powershell,Wildcard,我有这个脚本,它工作得很好 $watcher = New-Object System.IO.FileSystemWatcher $watcher.Path = "C:\folder_to_scan\" $watcher.Filter = "*.nrrd" $watcher.IncludeSubdirectories = $true $watcher.EnableRaisingEvents = $true $action = { $path = $Event.SourceEven

我有这个脚本,它工作得很好

$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\folder_to_scan\"
$watcher.Filter = "*.nrrd"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true  

$action = 
{ 
    $path = $Event.SourceEventArgs.FullPath
    Write-Host "The file '$path' was $changeType at '$(Get-Date)'" -fore green             
}   
Register-ObjectEvent $watcher "Created" -Action $action
while ($true) {sleep 3600}
但是在我的
C:\folder\u to\u scan\
中有很多子目录,比如
00123
00245
56002
。。。在它们里面我有
\u目录\u到\u扫描

所以我尝试了这个
$watcher.Path=“C:\folder\u to\u scan\*\u DIRECTORY\u to\u scan\”
$watcher.Path=“C:\folder\u to\u scan\[0-9]*\u DIRECTORY\u to\u scan\”

但这是行不通的。在这种情况下可以使用通配符吗


如果没有,如何将多路径与
FileSystemWatcher
一起使用? 因为我发现我们可以用这个

Resolve-Path "C:\folder_to_scan\*\THE_DIRECTORY_TO_SCAN\" | Select -ExpandProperty Path

我不熟悉
FileSystemWatcher
,因此我假设您只需要将文件夹路径馈送到
$watcher.Path

我还假设你的代码已经运行了

尝试使用
Get ChildItem
并使用for循环:

$watcher = New-Object System.IO.FileSystemWatcher
Get-ChildItem "C:\folder_to_scan\" -Recurse | ? { $_.PSIsContainer -and $_.Name.EndsWith("THE_DIRECTORY_TO_SCAN")}| %{ 
    $watcher.Path = $_.FullName #change this
    $watcher.Filter = "*.nrrd"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true  
}
    $action = 
    { 
        $path = $Event.SourceEventArgs.FullPath
        Write-Host "The file '$path' was $changeType at '$(Get-Date)'" -fore green             
    }   
    Register-ObjectEvent $watcher "Created" -Action $action
    while ($true) {sleep 3600} 

我不熟悉
FileSystemWatcher
,因此我假设您只需要将文件夹路径馈送到
$watcher.Path

我还假设你的代码已经运行了

尝试使用
Get ChildItem
并使用for循环:

$watcher = New-Object System.IO.FileSystemWatcher
Get-ChildItem "C:\folder_to_scan\" -Recurse | ? { $_.PSIsContainer -and $_.Name.EndsWith("THE_DIRECTORY_TO_SCAN")}| %{ 
    $watcher.Path = $_.FullName #change this
    $watcher.Filter = "*.nrrd"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true  
}
    $action = 
    { 
        $path = $Event.SourceEventArgs.FullPath
        Write-Host "The file '$path' was $changeType at '$(Get-Date)'" -fore green             
    }   
    Register-ObjectEvent $watcher "Created" -Action $action
    while ($true) {sleep 3600} 

好的,我想澄清一下我刚才所说的。您只是在重复等待功能。让你明白我的意思:

    echo "This would be your code running."
    while ($true) {sleep -second 3}
这将打印一次句子,然后每次$true为true时,都会持续等待3秒钟

您需要做的是:

do{
   echo "This would be your code running."
   sleep -seconds 3
} while($true)
这将每3秒钟打印一次句子。
或者,在您的情况下,每3秒钟运行一次您的watcher。

好的,请澄清我刚才所说的内容。您只是在重复等待功能。让你明白我的意思:

    echo "This would be your code running."
    while ($true) {sleep -second 3}
这将打印一次句子,然后每次$true为true时,都会持续等待3秒钟

您需要做的是:

do{
   echo "This would be your code running."
   sleep -seconds 3
} while($true)
这将每3秒钟打印一次句子。
或者在您的情况下,每3秒运行一次watcher。

我没有找到完美的解决方案,所以我这样做了

if ( $path -like '*\THE_DIRECTORY_TO_SCAN\*' ) 
{ Write-Host "File created in \THE_DIRECTORY_TO_SCAN" -fore green }
else
{ Write-Host "File created NOT in \THE_DIRECTORY_TO_SCAN" -fore red }  

我没有找到一个完美的解决方案,所以我做了这个

if ( $path -like '*\THE_DIRECTORY_TO_SCAN\*' ) 
{ Write-Host "File created in \THE_DIRECTORY_TO_SCAN" -fore green }
else
{ Write-Host "File created NOT in \THE_DIRECTORY_TO_SCAN" -fore red }  

很抱歉,这怎么行?而($true){sleep3600}这只会让你的脚本等待永恒,因为$true永远都是真的。你想做的是一个do{}while(),所以你想做的是,扫描每一个子目录,它有一个类似“thethethethethedirectory\u to\u scan”的名字?我要做的是使用Get childitem并在foldername中搜索所有具有该名称的路径。当您获得这些路径时,您将开始关注监视它们。
sleep 3600
每小时仅监视一次。我缩短了这段代码,因为它更复杂,我在
操作中做了很多事情。无论如何,如何给出
FileSystemWatcher
的路径列表?很抱歉,这是如何工作的?而($true){sleep3600}这只会让你的脚本等待永恒,因为$true永远都是真的。你想做的是一个do{}while(),所以你想做的是,扫描每一个子目录,它有一个类似“thethethethethedirectory\u to\u scan”的名字?我要做的是使用Get childitem并在foldername中搜索所有具有该名称的路径。当您获得这些路径时,您将开始关注监视它们。
sleep 3600
每小时仅监视一次。我缩短了这段代码,因为它更复杂,我在
操作中做了很多事情。无论如何,如何给
FileSystemWatcher
提供一个路径列表?我可以使用
Get ChildItem
Resolve path
检索路径列表,就像我在文章中解释的那样。但是如何给FileSystemWatcher提供多个参数呢?这就是我被困的地方。我是否必须使用多实例的
FileSystemWatcher
?这看起来一点也不优雅,近乎完美。唯一的问题是启动powershell脚本后创建文件夹的时间。它将不起作用…我试图将
while($true){sleep 3600}
放在最后一个
}
之后,但它不起作用是的,我只是再次尝试确认,是的,它在你收到任何错误消息之前起作用了吗?它在哪里停止工作?对于您的代码,这只适用于循环的第一个路径,因为脚本被困在循环中,因为
(我认为)。使用
while
outside,我没有任何错误,但它对每个已经存在的文件夹(不是新文件夹)都有效,但是
写入主机
被多次触发(与我认为给定路径的数量有关),我可以使用
Get ChildItem
Resolve path
检索路径列表,如我在帖子中解释的。但是如何给FileSystemWatcher提供多个参数呢?这就是我被困的地方。我是否必须使用多实例的
FileSystemWatcher
?这看起来一点也不优雅,近乎完美。唯一的问题是启动powershell脚本后创建文件夹的时间。它将不起作用…我试图将
while($true){sleep 3600}
放在最后一个
}
之后,但它不起作用是的,我只是再次尝试确认,是的,它在你收到任何错误消息之前起作用了吗?它在哪里停止工作?对于您的代码,这只适用于循环的第一个路径,因为脚本被困在循环中,因为
(我认为)。使用
while
outside,我没有任何错误,但是它对每个已经存在的文件夹(不是新文件夹)都有效,但是
写入主机
被多次触发(我认为与给定路径的数量有关),您是对的,但是观察者不是这样工作的。你可以查看更多细节。你是对的,但观察者不是这样工作的。您可以查看此以了解更多详细信息