移动文件在 powershell 中不起作用
Moving files not woking in powershell
我有一个代码:
cls
$folderPath = 'Y:\lit\Retail\abc\abc\FTP-FromClient\AMS22-03-23'
$files = Get-ChildItem $folderPath
$destination='Y:\lit\Retail\abc\abc\FTP-FromClient\AMS22-03-23\bz2'
$leftfiles=('20220322054419',
'20220322083959',
'20220322084715',
'20220322085207',
'20220322085250',
'20220322085858',
'20220322090236',
'20220322090410',
'20220322090450'
'20220322170306')
foreach($j in $leftfiles)
{
foreach($f in $files)
{
if ($f -contains $j)
{
Move-Item $f.FullName $destination
}
}
}
在这个 $folderPath 中我有一些文件:
$folderPath = 'Y:\lit\Retail\abc\abc\FTP-FromClient\AMS22-03-23'
文件命名有一些命名约定,与数组中的数字相匹配:
Colly_20220322054419.dat.bz2
Colly_1_20220322084715.dat.bz2
Colly_3_20220322085207.dat
如果文件名包含数组元素中的数字,我只需要将所有文件移动到destination
。所以我尝试使用 -contains
移动文件,但它不起作用:
foreach($j in $leftfiles)
{
foreach($f in $files)
{
if ($f -contains $j)
{
Move-Item $f.FullName $destination
}
}
}
如果您使用 Where-Object
子句过滤要使用正则表达式 -match
运算符
移动的文件,则可以简化您的代码
$folderPath = 'Y:\lit\Retail\abc\abc\FTP-FromClient\AMS22-03-23'
$destination = 'Y:\lit\Retail\abc\abc\FTP-FromClient\AMS22-03-23\bz2'
$leftfiles = '20220322054419', '20220322083959','20220322084715','20220322085207','20220322085250',
'20220322085858','20220322090236','20220322090410','20220322090450','20220322170306'
# first make sure the destination folder exists
$null = New-Item -Path $destination -ItemType Directory -Force
# join the items from $leftfiles with a pipe symbol (=the regex 'OR')
$regex = $leftfiles -join '|'
Get-ChildItem -Path $folderPath -File |
Where-Object { $_.BaseName -match $regex } |
Move-Item -Destination $destination
如果您想先对此进行测试,请将开关 -WhatIf
附加到 Move-Item
行。这样,实际上没有任何东西被移动。仅在控制台中显示将被移动的内容
我有一个代码:
cls
$folderPath = 'Y:\lit\Retail\abc\abc\FTP-FromClient\AMS22-03-23'
$files = Get-ChildItem $folderPath
$destination='Y:\lit\Retail\abc\abc\FTP-FromClient\AMS22-03-23\bz2'
$leftfiles=('20220322054419',
'20220322083959',
'20220322084715',
'20220322085207',
'20220322085250',
'20220322085858',
'20220322090236',
'20220322090410',
'20220322090450'
'20220322170306')
foreach($j in $leftfiles)
{
foreach($f in $files)
{
if ($f -contains $j)
{
Move-Item $f.FullName $destination
}
}
}
在这个 $folderPath 中我有一些文件:
$folderPath = 'Y:\lit\Retail\abc\abc\FTP-FromClient\AMS22-03-23'
文件命名有一些命名约定,与数组中的数字相匹配:
Colly_20220322054419.dat.bz2
Colly_1_20220322084715.dat.bz2
Colly_3_20220322085207.dat
如果文件名包含数组元素中的数字,我只需要将所有文件移动到destination
。所以我尝试使用 -contains
移动文件,但它不起作用:
foreach($j in $leftfiles)
{
foreach($f in $files)
{
if ($f -contains $j)
{
Move-Item $f.FullName $destination
}
}
}
如果您使用 Where-Object
子句过滤要使用正则表达式 -match
运算符
$folderPath = 'Y:\lit\Retail\abc\abc\FTP-FromClient\AMS22-03-23'
$destination = 'Y:\lit\Retail\abc\abc\FTP-FromClient\AMS22-03-23\bz2'
$leftfiles = '20220322054419', '20220322083959','20220322084715','20220322085207','20220322085250',
'20220322085858','20220322090236','20220322090410','20220322090450','20220322170306'
# first make sure the destination folder exists
$null = New-Item -Path $destination -ItemType Directory -Force
# join the items from $leftfiles with a pipe symbol (=the regex 'OR')
$regex = $leftfiles -join '|'
Get-ChildItem -Path $folderPath -File |
Where-Object { $_.BaseName -match $regex } |
Move-Item -Destination $destination
如果您想先对此进行测试,请将开关 -WhatIf
附加到 Move-Item
行。这样,实际上没有任何东西被移动。仅在控制台中显示将被移动的内容