如何在Powershell中迭代文件?
我正在尝试将文件从一个文件夹移动到另一个文件夹,下面是我想出的PowerShell代码:如何在Powershell中迭代文件?,powershell,pattern-matching,Powershell,Pattern Matching,我正在尝试将文件从一个文件夹移动到另一个文件夹,下面是我想出的PowerShell代码: $folder = 'C:\test' $filter = '*.*' # <-- set this according to your requirements $destination = 'C:\Folder1' $fsw = New-Object IO.FileSystemWatcher $folder, $filter -Propert
$folder = 'C:\test'
$filter = '*.*' # <-- set this according to your requirements
$destination = 'C:\Folder1'
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
IncludeSubdirectories = $true # <-- set this according to your requirements
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
$path = $Event.SourceEventArgs.FullPath
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' was $changeType at $timeStamp"
#$UNI_PRINT = Select-String -Path C:\Test\*.Print_Job -Pattern "SATO"\\
$file = Get-Content -Path $folder
$containsWord = $file | %{$_ -match "SATO"}
if ($containsWord -contains $true)
{
Write-Host Contains String
Move-Item $path -Destination $destination -Force -Verbose # Force will overwrite files with same name
}
else
{
Write-Host Not Contains String
}
}
$folder='C:\test'
$filter='*.'#类似的方法应该可以
$directory_source = '<path>'
$directory_target_motorola = '<path>'
$directory_target_sato = '<path>'
$directory_target_zebra = '<path>'
$files = $(ls $directory)
foreach ($file in $files) {
if ( $(gc $file | Select-String "motorola" -Quiet) ) {
mv -Path $file.Fullname -Destination $directory_target_motorola
} elseif ( $(gc $file | Select-String "sato" -Quiet) ) {
mv -Path $file.Fullname -Destination $directory_target_sato
} elseif ( $(gc $file | Select-String "zebra" -Quiet) ) {
mv -Path $file.Fullname -Destination $directory_target_zebra
}
}
$directory\u source=''
$directory\u target\u motorola=''
$directory\u target\u sato=''
$directory\u target\u zebra=''
$files=$(ls$目录)
foreach($files中的文件){
if($(gc$文件|选择字符串“motorola”-Quiet)){
mv-Path$file.Fullname-Destination$directory\u target\u motorola
}elseif($(gc$文件|选择字符串“sato”-Quiet)){
mv-路径$file.Fullname-目标$directory\u target\u sato
}elseif($(gc$文件|选择字符串“zebra”-安静)){
mv-Path$file.Fullname-Destination$directory\u target\u zebra
}
}
请注意,我没有尝试运行此操作
祝你好运。像这样的东西应该有用
$directory_source = '<path>'
$directory_target_motorola = '<path>'
$directory_target_sato = '<path>'
$directory_target_zebra = '<path>'
$files = $(ls $directory)
foreach ($file in $files) {
if ( $(gc $file | Select-String "motorola" -Quiet) ) {
mv -Path $file.Fullname -Destination $directory_target_motorola
} elseif ( $(gc $file | Select-String "sato" -Quiet) ) {
mv -Path $file.Fullname -Destination $directory_target_sato
} elseif ( $(gc $file | Select-String "zebra" -Quiet) ) {
mv -Path $file.Fullname -Destination $directory_target_zebra
}
}
$directory\u source=''
$directory\u target\u motorola=''
$directory\u target\u sato=''
$directory\u target\u zebra=''
$files=$(ls$目录)
foreach($files中的文件){
if($(gc$文件|选择字符串“motorola”-Quiet)){
mv-Path$file.Fullname-Destination$directory\u target\u motorola
}elseif($(gc$文件|选择字符串“sato”-Quiet)){
mv-路径$file.Fullname-目标$directory\u target\u sato
}elseif($(gc$文件|选择字符串“zebra”-安静)){
mv-Path$file.Fullname-Destination$directory\u target\u zebra
}
}
请注意,我没有尝试运行此操作
祝你好运。编辑现在适用于需求
- 在Select字符串和
找到的文件和匹配的值存储在哈希表中
- 最后,对哈希表进行迭代,使用
Get Variable
对目标文件夹进行评估,如果不存在,则创建目标文件夹,并移动文件(可选-whatif)
编辑现在适用于需求
- 在Select字符串和
找到的文件和匹配的值存储在哈希表中
- 最后,对哈希表进行迭代,使用
Get Variable
对目标文件夹进行评估,如果不存在,则创建目标文件夹,并移动文件(可选-whatif)