【问题标题】:PowerShell Move-Item $filenamePowerShell 移动项目 $filename
【发布时间】:2015-06-09 01:19:19
【问题描述】:

我搜索了,我用谷歌搜索了.. 差点把我的头砸在桌子上

这怎么行不通?

move-Item  $path$file $targetdir

它给了我一个错误 Move-Item : 指定路径 C:\Repository\test.csv 处的对象 不存在。

现在,如果我调试它并使用

输出
write-output move-Item  $path$file $targetdir

并获取该输出并将其粘贴(带有路径和目标的文件名)它可以工作!

相信我,文件就在那里。 =\


代码如下

    $path = 'C:\test\'
$TimeStamp = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
$LogFile = Get-Date -Format "MM_dd_yyyy"
$targetdir = "C:\test\Uploaded\"

#Get-ChildItem -path $path\* -Include *.csv | foreach-object {$_.Fullname} | Format-Table name -hidetableheaders  | Out-File $path\list.txt

Get-ChildItem -path $path\* -Include *.csv | Format-Table name -hidetableheaders  | Out-File $path\list2.txt
get-content C:\test\list2.txt | where {$_ -ne ""} | out-file C:\test\list.txt
Remove-Item  C:\test\list2.txt

$list = get-content C:\test\list.txt 


foreach ($file in $list)
{
    $ftp = "ftp://REMOVED/$file"
    "ftp url: $ftp"
    $webclient = New-Object System.Net.WebClient
    $uri = New-Object System.Uri($ftp)
    "Uploading $file..."

    $succeeded = $true;
    &   {
    trap { $script:succeeded = $false; continue }
    $webclient.UploadFile($uri, $path+$file)
        }
if ($succeeded) 
        { 

        echo $file 'Was successfully uploaded!' $Timestamp >> logfile$LogFile.log
        move-Item  -path $path$file -destination $targetdir
        #test-path $path$file
        } 

    else 
        { 

        echo $file 'Was not successfully uploaded, will retry later' $Timestamp >> logfile$LogFile.log

        }   


}

exit

【问题讨论】:

  • 你试过在"$path$file"周围加上双引号来调用字符串插值吗?
  • @TheMadTechnician 正确我有,同样的错误。
  • 如果你这样做Test-Path "$path$file"会发生什么?我正在考虑您问题中的最后一行
  • @TheMadTechnician 输出 = True,不带 " 和带
  • 我刚试了一下,无法重现问题。在这一点上,我们需要更多信息。什么版本的 PowerShell? $path、$file 和 $targetdir 是字符串吗?文件信息对象? PSCustom 对象?我使用了这些变量,将它们设置为字符串,然后毫无问题地运行您的命令。 $path='c:\temp\' $file='list.csv' 和 $targetdir='c:\temp\test' 正常工作(并移动了文件)

标签: powershell


【解决方案1】:

基础是:

  • 移动之前的测试路径(文件和目标)
  • 移动文件,确保您有权限(强制移动)

所以:

echo $targetdir
echo "$path$file"
if (!(Test-Path $targetdir)) {
    New-Item -ItemType directory $targetdir
}
if(Test-Path "$path$file") { 
    Move-Item "$path$file" $targetdir -Force
} else {
    echo "file does not exist"
}

如果你循环一个集合,你必须使用对象的“.FullName”属性:

Get-ChildItem $path | ForEach-Object { Move-Item $_.FullName $targetdir -Force }

【讨论】:

    【解决方案2】:

    目标目录是否已经存在?如果目标目录不存在,我相信 Move-Item 会失败。如果是这种情况,您可以简单地预先测试目录是否存在,然后根据需要创建。

    If (!(Test-Path -Path $targetdir)) {
        New-Item -ItemType directory -Path $targetdir
    }
    

    【讨论】:

    • 是的,如果我不使用变量但使用精确的字符串@kris powell,目录就可以正常工作
    【解决方案3】:

    这对我有用。谢谢@TheMadTechnician。希望对大家有帮助

    $TimeStamp = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
    $LogFile = Get-Date -Format "MM_dd_yyyy"
    
    $path='C:\test\'
    $targetDir = 'C:\test\Uploaded\'
    $fileList = Get-ChildItem $path*.csv
    If(!(Test-Path $TargetDir)){New-Item -ItemType Directory -Path $TargetDir|Out-Null}
    $fileList | Select -ExpandProperty Name | Out-File 'C:\test\list.txt'
    
    $list = get-content C:\test\list.txt 
    foreach ($file in $list)
    {
        $ftp = "ftp://REMOVED/$file"
        "ftp url: $ftp"
        $webclient = New-Object System.Net.WebClient
        $uri = New-Object System.Uri($ftp)
        "Uploading $file..."
    
        $succeeded = $true;
        &   {
        trap { $script:succeeded = $false; continue }
        $webclient.UploadFile($uri, $path+$file)
            }
    if ($succeeded) 
            { 
    
            echo $file 'Was successfully uploaded!' $Timestamp >> logfile$LogFile.log
            move-Item  -path $path$file -destination $targetdir$Timestamp"_"$file
            #test-path $path$file
            } 
    
        else 
            { 
    
            echo $file 'Was not successfully uploaded, will retry later' $Timestamp >> logfile$LogFile.log
    
            }   
    
    
    }
    
    exit
    

    【讨论】:

    • 很高兴你让它工作了,只是为了学习一两种技术,看看我刚刚添加到我的答案中的内容,也许会有一两件事你想要有时合并到您的脚本中。
    【解决方案4】:

    那么这个怎么样:

    ForEach($File in $List){
        Join-Path $path $file | Move-Item -Dest $Targetdir
    }
    

    编辑:另外......你创建的list.txt,它困扰着我,所以我不得不发表评论。 Format-Table 应该用于格式化文本,而不是用于选择要输出到文件的值。有更好的方法可以做到这一点,请考虑以下替代方法:

    Get-ChildItem "$path*.csv" | Select -ExpandProperty Name | Out-File $pathlist.txt
    

    既然您说$path = 'C:\test\',您在其中添加了额外的反斜杠,这可能会导致某些命令出现问题。

    Edit2:好的,如果这不起作用,为什么不使用文件本身,而不是输出到文件,从该文件导入,然后处理。

    $path='c:\test\'
    $TargetDir = 'c:\test\NewDir'
    $FileList = Get-ChildItem $path*.csv
    If(!(Test-Path $TargetDir)){New-Item -ItemType Directory -Path $TargetDir|Out-Null}
    $FileList | Move-Item -Destination $TargetDir
    

    如果你真的想要这些文件名的列表,只需通过管道 $FileList 到 Select 然后到 Out-File

    $FileList | Select -ExpandProperty Name | Out-File 'C:\Test\list.txt'
    

    来,看看这个,看看有没有你喜欢的。我做了一些更改,例如在开头声明所有内容的路径,将 WebClient 对象的创建移到循环之外,并更改了内容在屏幕上的显示方式。另外,我跳过了整个导出到文本文件并重新导入它的过程。

    $path = 'C:\test'
    $ftpaddr = 'ftp://ftp.example.com/uploads'
    $TimeStamp = Get-Date -Format "MM/dd/yyyy hh:mm:ss tt"
    $LogFile = Get-Date -Format "MM_dd_yyyy"
    
    $LogDir = "C:\Test\Logs"
    If(!(test-path $LogDir)){New-Item -ItemType Directory -Path $LogDir | Out-Null}
    $targetdir = 'C:\test\Uploaded'
    If(!(test-path $targetdir)){New-Item -ItemType Directory -Path $targetdir | Out-Null}
    
    $list = Get-ChildItem -path $path\* -Include *.csv
    
    $webclient = New-Object System.Net.WebClient
    
    "ftp url: $ftpaddr"
    
    foreach ($file in ($list|select -ExpandProperty Name))
    {
        $uri = New-Object System.Uri(("$ftpaddr/$file"))
    
        Write-Host "Uploading $file... " -NoNewline -ForegroundColor White
    
        $succeeded = $true
        &   {
        trap { $script:succeeded = $false; continue }
        $webclient.UploadFile($uri, "$Path\$file")
            }
    
    if ($succeeded) 
            {
            Write-Host "Success!" -ForegroundColor Green
            "$Timestamp`t$File was successfully uploaded!" | Out-File "$logdir\logfile$LogFile.log" -Append
            move-Item  -path "$path\$file" -destination $targetdir
            } 
    
        else 
            { 
            Write-Host "Failed! Will retry later." -ForegroundColor Red
            "$Timestamp`t$File was not successfully uploaded, will retry later" | Out-File "$logdir\logfile$LogFile.log" -Append
            }   
    
    }
    

    【讨论】:

    • 查看上面我创建的完整代码。让我知道这是否有帮助。这将有助于更好地理解它。我基本上是在获得列表并将文件移动到 Uploaded 文件夹后上传。
    • 我想我想多了=|它变得一团糟
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多