【问题标题】:Powershell - Rename File and UploadPowershell - 重命名文件并上传
【发布时间】:2021-09-28 01:05:16
【问题描述】:

我有以下脚本,到目前为止,它可以很好地满足我们的需要,但是由于文件名已更改而出现错误。

使用以下脚本,由于文件名已更改,它会提供找不到路径错误。 这个我哪里错了?

## Add ShareFile PowerShell Snap-in
Add-PSSnapin ShareFile

## Create new authentiation file
#New-SfClient -Name "C:\Sharefile\SVCACC.sfps" -Account midl

## Variables ##
$OutputAppReqFID = "fo4a3b58-bdd6-44c8-ba11-763e211c183f"
$Project = 'M000'
$LocalPath = "\\file.server.au\$project\DATA\DATA CUSTODIAN\OUTPUT\"
$sfClient = Get-SfClient -Name C:\sharefile\SVCACC.sfps
$OutputAppReqFID_URL = (Send-SfRequest $sfClient -Entity Items -id $OutputAppReqFID).Url


## Create PS Drive ##
New-PSDrive -Name "sfDrive-$($project)" -PSProvider ShareFile -Client $sfClient -Root "\" -RootUri $OutputAppReqFID_URL

## Copy all files from folders to ShareFile
foreach ($file in Get-ChildItem -Path $LocalPath -Recurse -Force | Where-Object {$_.Mode -ne "d-----"} | Select FullName -ExpandProperty FullName) { 
     Get-ChildItem $file  -Recurse | Rename-Item -NewName { $_.Directory.Name+'_'+$_.Name}
     Copy-SfItem -Path $file -Destination "sfDrive-$($project):" 
    #remove-item $file
    }

<## Remove all folders from UNC directory
foreach ($folder in Get-childitem -Path $LocalPath -Recurse | Where-Object {$_.Mode -eq "d-----"} | Select-Object -ExpandProperty FullName) {
    remove-item $folder
    }
    #>

## Remove PS Drive ##
Remove-PSDrive "sfdrive-$($project)"

收到的错误如下:

Copy-SfItem : Cannot find path '\\file.server.au\M000\DATA\DATA CUSTODIAN\OUTPUT\New Text Document.txt' because it does not exist.
At line:43 char:6
+      Copy-SfItem -Path $file -Destination "sfDrive-$($project):"
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (\\file.serve...xt Document.txt:String) [Copy-SfItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,ShareFile.Api.Powershell.CopySfItem

【问题讨论】:

  • 你能分享你的错误吗?它通常会告诉您错误所在的行,我们可以指出它
  • 已更新以包含错误

标签: powershell citrix sharefile


【解决方案1】:

问题是您正在重命名 $file 然后尝试在下一行复制-SfItem -Path $file。这个对 $file 的引用仍然指向重命名之前的旧名称。您需要捕获新重命名的文件路径的路径,然后在 Copy-SfItem 命令中引用它

foreach ($file in Get-ChildItem -Path $LocalPath -Recurse -Force | 
        Where-Object { $_.Mode -ne 'd-----' } | 
            Select-Object -ExpandProperty FullName) {
                
    # capture reference to the new file name by using -PassThru switch with 
    # the Rename-Item cmdlet and saving in a variable ($renamedFile) 
    $renamedFile = Get-ChildItem $file -Recurse | 
        Rename-Item -NewName { $_.Directory.Name + '_' + $_.Name } -PassThru

    # Now we copy using our $newFile reference
    Copy-SfItem -Path $renamedFile -Destination "sfDrive-$($project):" 

    # followed by removing the file if needed
    # remove-item $renamedFile
}

这也可以清理一些。

  • -File 开关添加到Get-ChildItem 将只为我们提供不需要| Where-Object {$_.Mode -ne "d-----"} 的文件
  • foreach 末尾删除| Select-Object -ExpandProperty FullName 将消除在第一个foreach 循环中再次调用Get-ChildItem 的需要。
foreach ($file in Get-ChildItem -Path $LocalPath -Recurse -Force -File ) {

    # capture reference to the new file name by using -PassThru switch with 
    # the Rename-Item cmdlet and saving in a variable ($newFile) 
    $renamedFile = $file | Rename-Item -NewName { $_.Directory.Name + '_' + $_.Name } -PassThru

    # Now we copy using our $newFile reference
    Copy-SfItem -Path $renamedFile -Destination "sfDrive-$($project):" 

    # followed by removing the file if needed
    # remove-item $renamedFile
}

根据您的 cmets 更新

foreach ($file in Get-ChildItem -Path $LocalPath -Recurse -Force -File ) {

    $file = if ($file.Directory.FullName -ne $LocalPath) {
        # capture reference to the new file name by using -PassThru switch with 
        # the Rename-Item cmdlet and saving in a variable ($file) 
        $file | Rename-Item -NewName { $_.Directory.Name + '_' + $_.Name } -PassThru
    }
    else {
        #Pass thru $file with no changes
        $file
    }

    # Now we copy using our $file reference
    Copy-SfItem -Path $file -Destination "sfDrive-$($project):" 

    # followed by removing the file if needed
    # remove-item $file
}

【讨论】:

  • 谢谢丹尼尔,这可以说是一针见血,非常棒,非常感谢。我又学到了一件很棒的事!我现在需要更正的另一件事是该文件是否位于 OUTPUT 文件夹中,因此无需重命名根目录中的任何内容。我认为我需要在此添加一个 if 语句,该语句类似于此 if ($file.FullName eq "//UNCpath/OUTPUT") { do nothing } else { run rename script}
  • 不客气@AndrewWaters。查看我的更新以显示如何根据文件是否在根目录中有条件地重命名文件。
  • 嘿@Daniel,再次感谢,我非常接近我必须让它工作的东西。我在上面应用了您的修改,但是它似乎仍然在重命名 /OUTPUT 目录中的任何内容我应该使用 //file.server.au/m000/data/data custodian/output (Root) 确认正在测试的目录这应该是唯一不会重命名文件的文件夹。里面的每个文件夹都应该重命名
  • 尝试从$LocalPath = "\\file.server.au\$project\DATA\DATA CUSTODIAN\OUTPUT\" 末尾删除'\'
  • 这真是一针见血!在文件夹中创建文件夹时,例如 folder1/folder2/file.txt 它不会重命名 Folder1_Folder_2_file.txt 任何想法。我已经看了几个小时了,我似乎没有得到任何地方
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-21
  • 2013-07-02
  • 1970-01-01
  • 2015-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多