重命名文件(如果已存在)

时间:2018-08-29 23:12:40

标签: powershell copy rename auto-increment

如果文件重复,则该脚本可在复制文件时重命名文件。我需要先重命名当前目标文件,然后按原样复制源文件。有什么想法吗?

function Copy-FilesWithVersioning{
    Param(
        [string]$source,
        [string]$destination
    )
    Get-ChildItem -Path $source -File
        ForEach-Object {
            $destinationFile = Join-Path $destination $file.Name
            if ($f = Get-Item $destinationFile -EA 0) {
                # loop for number goes here
                $i = 1
                $newname = $f.Name -replace $f.BaseName, "$($f.BaseName)_$I")
                Rename-Item $destinationFile $newName
            }
            Copy-Item $_ $destination
        }
}

Copy-FilesWithVersioning c:\scripts\Source c:\scripts\DestinationA

错误:

At line:10 char:53
+             if($f = Get-Item $destinationFile -EA 0){
+                                                     ~
Missing closing '}' in statement block or type definition.
At line:8 char:23
+         ForEach-Object{
+                       ~
Missing closing '}' in statement block or type definition.
At line:2 char:34
+ function Copy-FilesWithVersioning{
+                                  ~
Missing closing '}' in statement block or type definition.
At line:13 char:77
+ ...         $newname = $f.Name -replace $f.BaseName, "$($f.BaseName)_$I")
+                                                                         ~
Unexpected token ')' in expression or statement.
At line:15 char:13
+             }
+             ~
Unexpected token '}' in expression or statement.
At line:17 char:9
+         }
+         ~
Unexpected token '}' in expression or statement.
At line:18 char:1
+ }
+ ~
Unexpected token '}' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndCurlyBrace

2 个答案:

答案 0 :(得分:1)

您看到的错误是由以下行中的假括弧引起的:

$newname = $f.Name -replace $f.BaseName, "$($f.BaseName)_$I")

从行尾删除括号,这些错误将消失。

尽管您的代码中还有其他几个错误,所以即使修复了该错误,代码仍然无法正常工作。

  • 您缺少Get-ChildItemForEach-Object之间的管道。将一个cmdlet的输出传递到另一个cmdlet时需要使用此

    Get-ChildItem -Path $source -File |
        ForEach-Object {
            ...
        }
    
  • 变量$file未定义。在PowerShell管道中,您要使用“当前对象”变量($_)。更改此行

    $destinationFile = Join-Path $destination $file.Name
    

    进入

    $destinationFile = Join-Path $destination $_.Name
    
  • 语句中的
  • $_

    Copy-Item $_ $destination
    

    被扩展为仅文件名,而不是完整路径。将其更改为

    Copy-Item $_.FullName $destination
    

    更好的是,将Copy-Item语句移到{{1}之后之后,这样就不必首先明确指定源(cmdlet从管道):

    ForEach-Object

    请注意,您必须 将当前对象输出回管道,并指定目的地作为命名参数(Get-ChildItem ... | ForEach-Object { ... $_ # need this line to pass the current object back into the pipeline } | Copy-Item -Destination $destination ),以便后者工作。

  • 您检查目标文件夹中是否存在文件有点尴尬。请改用-Destination $destination。您可以从当前对象构造新文件名。

    Test-Path

答案 1 :(得分:0)

尝试通过以下链接提供代码:https://www.pdq.com/blog/copy-individual-files-and-rename-duplicates/

type RequireAllPropertiesContractResolver() =
    inherit CamelCasePropertyNamesContractResolver()

    override __.CreateProperty(memb, serialization) =
        let prop = base.CreateProperty(memb, serialization)
        let isRequired = not (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() = typedefof<option<_>>)
        if isRequired then prop.Required <- Required.Always
        prop