与PowerShell匹配的模式1

所有,

我是新的Windowspipe理员types的东西,但我熟悉的Unix / Linux等我有一个简单的PowerShell 1脚本,应该只复制文件符合这个命名约定:

Daily_Reviews[1099-9987].journal 

$ source中的模式导致错误:“无法findpath的一部分..”我需要知道如何在PowerShell环境中进行模式匹配

这是我的代码:

 $source = "C:\Users\Tom\Daily_Reviews\[\d\d\d\d\-\d\d\d\d\]\.journal" $destination ="C:\Users\Tom\Processed\" if(-not(Test-Path $destination)){mkdir $destination | out-null} Copy-Item $source $destination 

你可以使用-match来比如if ( "$source" -match "Daily_Reviews*" ) { echo Match! } if ( "$source" -match "Daily_Reviews*" ) { echo Match! }

这接受正则expression式。

无论如何,你要做的事情应该可以用for循环来完成,比如说

ls Daily_Reviews *> file.txt; 获取内容file.txt同时读取input,做一个副本等…

(我不是一个PowerShell的家伙)

你可能会尝试更多这样的东西:

 $source = Join-Path "C:\Users" $env:username $destination = Join-Path $source "Processed" if (![System.IO.Directory]::Exists($destination)) {[System.IO.Directory]::CreateDirectory($destination)} dir -Path $source -file -filter "*.journal" | where { $_ -match "Daily_Reviews\d{4}" } | ForEach-Object { Move-Item -Path $_.fullname -Destination $destination } 

但是,这可能不适用于PS1。 这可能是值得升级到Powershell 2?

这是假设你正在寻找转移多个文件 – 你可以平等地使用不同的潜在文件名填充一个数组,并单独检查每个文件,但是创build这个variables是不行的(据我所知) 。