我尝试用powershell重命名csv,然后在没有文件的情况下自动将它移动到另一个文件夹。最初,csv名称看起来如下: import_ 9999 _2020-08-13_132238.csv,但带有9999的部分也只能包含2或3位数字。我的实际代码如下:
#Import of path and target-path
$path = "\\network-path\subfolder\subfolder\subfolder\subfolder\subfolder1\"
$target_path = "\\network-path\subfolder\subfolder\subfolder\subfolder\subfolder2\"
#endless loop
$a=$true
while($a -eq $true){
$Files = gci $path
$TargetFiles = gci $target_path
#wait 5 minutes if path is empty
if(($Files).Count -eq 0){
sleep -Seconds 300
}
#if path is filled with one or more files
else {
#if file in target-path is processed (from another program)
if(($TargetFiles).count -eq 0){
#rename and move the latest file
get-childitem -path $path -Filter "import_*.csv"|
where-object { -not $_.PSIsContainer } |
sort-object -Property $_.CreationTime |
select-object -last 1 |
Rename-Item -NewName {($_.Name.Substring(0,($_.Name.Length)-22))+".csv"} |
Move-Item -Destination $target_path +"$($_.Name).csv"
}
sleep -Seconds 20
}
}它部分工作,并重命名csv,但它没有将它移动到目标路径。路径是正确的,我从复制了它。有什么想法吗,为什么这个程序不能完全工作?谢谢
发布于 2020-10-19 06:47:53
如果要将其传递到管道中,请将-Passthru添加到Rename-Item cmdlet
Somefile.txt | Rename-Item -NewName {$_.basename + "abc" + $_.ext} | # nothing in the pipeline
Somefile.txt | Rename-Item -NewName {$_.basename + "abc" + $_.ext} -Passthru | # now the new named fileinfo object is in the pipeline, contained in automatic variable $_ https://stackoverflow.com/questions/64422033
复制相似问题