Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
复制文件重命名(如果已存在PowerShell)_Powershell_Copy_Rename_Auto Increment - Fatal编程技术网

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

复制文件重命名(如果已存在PowerShell),powershell,copy,rename,auto-increment,Powershell,Copy,Rename,Auto Increment,如果文件重复,此脚本可以在复制文件时重命名文件。我需要先重命名当前目标文件,然后按原样复制源文件。有什么想法吗 function Copy-FilesWithVersioning{ Param( [string]$source, [string]$destination ) Get-ChildItem -Path $source -File ForEach-Object { $destinationF

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

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 第10行字符:53 +如果($f=获取项目$destinationFile-EA 0){ + ~ 语句块或类型定义中缺少结尾“}”。 第8行字符:23 +ForEach对象{ + ~ 语句块或类型定义中缺少结尾“}”。 第2行字符:34 +使用版本控制功能复制文件{ + ~ 语句块或类型定义中缺少结尾“}”。 第13行字符:77 + ... $newname=$f.Name-替换$f.BaseName,“$($f.BaseName)$I”) + ~ 表达式或语句中出现意外标记“)”。 第15行字符:13 + } + ~ 表达式或语句中出现意外标记“}”。 第17行字符:9 + } + ~ 表达式或语句中出现意外标记“}”。 第18行字符:1 + } + ~ 表达式或语句中出现意外标记“}”。 +CategoryInfo:ParserError:(:)[],ParentContainerErrorRecordException +FullyQualifiedErrorId:MissingEndCurlyBrace
您看到的错误是由此行中假右括号引起的:

$newname = $f.Name -replace $f.BaseName, "$($f.BaseName)_$I")
删除行末尾的括号,这些错误将消失

不过,代码中还有一些其他错误,因此即使修复了这些错误,代码仍然无法工作

  • Get ChildItem
    ForEach对象之间缺少管道。它是将一个cmdlet的输出传递给另一个cmdlet所必需的

    Get-ChildItem -Path $source -File |
        ForEach-Object {
            ...
        }
    
  • 变量
    $file
    未定义。在PowerShell管道中,您希望使用“current object”变量(
    $\uuu
    )。换行

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

  • 语句中的$

    Copy-Item $_ $destination
    
    仅扩展为文件名,而不是完整路径。换成

    Copy-Item $_.FullName $destination
    
    更好的是,将
    Copy Item
    语句移动到
    ForEach对象之后,这样您就不需要首先明确指定源(cmdlet从管道读取输入):

    请注意,必须将当前对象输出回管道,并将目标指定为命名参数(
    -destination$destination
    ),后者才能工作

  • 检查目标文件夹中是否存在文件有点麻烦。改用
    测试路径
    。可以从当前对象构造新文件名

    if (Test-Path -LiteralPath $destinationFile) {
        $i = 1
        Rename-Item $destinationFile ($_.BaseName + "_$i" + $_.Extension)
    }
    
尝试此链接中的代码:


我遇到了一个类似的问题,我总是创建一个新文件,在名称中插入一个日期/时间格式为yyyy-MM-ddTHH:MM:ss的节点。所以foo.txt首先被复制为foo.2018-08-29T12:34.45.txt,所以我有一个文件的存档副本,然后被复制为foo.txt。或者,您可以调用TEST-PATH并在复制文档时进行类似的重命名。取决于您期望的副本数量。谢谢,不退款,我们有一个应用程序正在寻找一个特定的文件名,它会将其拾取。因此,当我添加文件的新版本时,它会忽略名称后面带有-#的文件,并仅导入新文件。我同意整个流程需要改进,但这不是我的部门。除非
#
仅代表实际的个位数,否则您可以用勾号(Get Date)替换。勾号作为破折号后的部分。您可以随时将其转换回实际日期,并且仍然可以按日期/时间对其进行排序。
Get-ChildItem ... | ForEach-Object {
    ...
    $_   # need this line to pass the current object back into the pipeline
} | Copy-Item -Destination $destination
if (Test-Path -LiteralPath $destinationFile) {
    $i = 1
    Rename-Item $destinationFile ($_.BaseName + "_$i" + $_.Extension)
}
$SourceFile = "C:\Temp\File.txt"
$DestinationFile = "C:\Temp\NonexistentDirectory\File.txt"

If (Test-Path $DestinationFile) {
    $i = 0
    While (Test-Path $DestinationFile) {
        $i += 1
        $DestinationFile = "C:\Temp\NonexistentDirectory\File$i.txt"
    }
} Else {
    New-Item -ItemType File -Path $DestinationFile -Force
}

Copy-Item -Path $SourceFile -Destination $DestinationFile -Force