如何修改PowerShell脚本实现文件扁平化后上传至Citrix ShareFile
调整后完整脚本
## Add ShareFile PowerShell Snap-in Add-PSSnapin ShareFile ## Create new authentication file #New-SfClient -Name "C:\Sharefile\SVCACC.sfps" -Account aws ## Variables ## $OutputAppReqFID = "fo4a3b58-bdd6-44c8-ba11-763e211c183f" $Project = 'A001' $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 ## 本地文件扁平化处理逻辑 # 递归提取所有层级的文件 $allFiles = Get-ChildItem -Path $LocalPath -File -Recurse foreach ($file in $allFiles) { # 重名文件自动加数字后缀,避免覆盖 $targetPath = Join-Path -Path $LocalPath -ChildPath $file.Name $suffix = 1 while (Test-Path -Path $targetPath) { $newName = "{0}_{1}{2}" -f $file.BaseName, $suffix, $file.Extension $targetPath = Join-Path -Path $LocalPath -ChildPath $newName $suffix++ } # 移动文件到源路径根目录 Move-Item -Path $file.FullName -Destination $targetPath -Force } # 从最底层开始删除所有空文件夹 Get-ChildItem -Path $LocalPath -Directory -Recurse | Sort-Object -Property FullName -Descending | ForEach-Object { if ((Get-ChildItem -Path $_.FullName -File -Recurse | Measure-Object).Count -eq 0) { Remove-Item -Path $_.FullName -Force } } ## Create PS Drive ## New-PSDrive -Name "sfDrive-$($project)" -PSProvider ShareFile -Client $sfClient -Root "\" -RootUri $OutputAppReqFID_URL ## 仅上传根目录下的所有文件,上传完成后清理本地源文件 Get-ChildItem -Path $LocalPath -File | ForEach-Object { Copy-SfItem -Path $_.FullName -Destination "sfDrive-$($project):" Remove-Item -Path $_.FullName -Force } ## Remove PS Drive ## Remove-PSDrive "sfdrive-$($project)"
核心修改说明
- 新增文件扁平化处理模块:递归拉取所有层级的文件统一移动到源路径根目录,自带重名检测逻辑,不会出现文件覆盖问题
- 新增空文件夹清理逻辑:从最深层目录开始扫描删除空文件夹,不会残留冗余目录结构
- 上传逻辑调整为仅读取根目录下的单个文件上传,不会把文件夹结构传递到ShareFile,符合业务要求的工作流触发规则
内容的提问来源于stack exchange,提问作者Andrew Waters
相关产品推荐
相关产品推荐

