首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >移动项不移动文件

移动项不移动文件
EN

Stack Overflow用户
提问于 2020-10-19 06:15:13
回答 1查看 102关注 0票数 0

我尝试用powershell重命名csv,然后在没有文件的情况下自动将它移动到另一个文件夹。最初,csv名称看起来如下: import_ 9999 _2020-08-13_132238.csv,但带有9999的部分也只能包含2或3位数字。我的实际代码如下:

代码语言:javascript
复制
#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,但它没有将它移动到目标路径。路径是正确的,我从复制了它。有什么想法吗,为什么这个程序不能完全工作?谢谢

EN

回答 1

Stack Overflow用户

发布于 2020-10-19 06:47:53

如果要将其传递到管道中,请将-Passthru添加到Rename-Item cmdlet

代码语言:javascript
复制
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 $_ 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64422033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档