我试图复制文件夹和子文件夹中的所有文件,但我得到的代码只复制主文件夹中的文件,它不复制子文件夹中的文件。在目的地,我不想维护原始文件的文件夹结构,我只想把所有原始文件放在一个特定的目标文件夹中。这是我的代码:
Powershell -NoL -NoP -C "&{$ts=New-TimeSpan -M 300;"^
 "Get-ChildItem "C:\Origin" -Filter '*.dat'|?{"^
 "$_.LastWriteTime -gt ((Get-Date)-$ts)}|"^
 %%{Copy-Item $_.FullName 'C:\Destination'}}"有人能帮帮我吗?提前谢谢。
发布于 2022-04-29 10:02:59
这里有一个修改过的脚本,您可以将其保存为"Copy-Unique.ps1“,您可以从批处理文件中运行。
function Copy-Unique {
    # Copies files to a destination. If a file with the same name already exists in the destination,
    # the function will create a unique filename by appending '(x)' after the name, but before the extension. 
    # The 'x' is a numeric sequence value.
    [CmdletBinding(SupportsShouldProcess)]  # add support for -WhatIf switch
    Param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
        [Alias("Path")]
        [ValidateScript({Test-Path -Path $_ -PathType Container})]
        [string]$SourceFolder,
        [Parameter(Mandatory = $true, Position = 1)]
        [string]$DestinationFolder,
        [Parameter(Mandatory = $false)]
        [int]$NewerThanMinutes = -1,
        [Parameter(Mandatory = $false)]
        [string]$Filter = '*',
        [switch]$Recurse
    )
    # create the destination path if it does not exist
    if (!(Test-Path -Path $DestinationFolder -PathType Container)) {
        Write-Verbose "Creating folder '$DestinationFolder'"
        $null = New-Item -Path $DestinationFolder -ItemType 'Directory' -Force
    }
    # get a list of file FullNames in this source folder
    $sourceFiles = @(Get-ChildItem -Path $SourceFolder -Filter $Filter -File -Recurse:$Recurse)
    # if you want only files not older than x minutes, apply an extra filter
    if ($NewerThanMinutes -gt 0) {
        $sourceFiles = @($sourceFiles | Where-Object { $_.LastWriteTime -gt (Get-Date).AddMinutes(-$NewerThanMinutes) })
    }
    foreach ($file in $sourceFiles) {
        # get an array of all filenames (names only) of the files with a similar name already present in the destination folder
        $destFiles = @((Get-ChildItem $DestinationFolder -File -Filter "$($file.BaseName)*$($file.Extension)").Name)
        # for PowerShell version < 3.0 use this
        # $destFiles = @(Get-ChildItem $DestinationFolder -Filter "$baseName*$extension" | Where-Object { !($_.PSIsContainer) } | Select-Object -ExpandProperty Name)
        # construct the new filename
        $newName = $file.Name
        $count = 1
        while ($destFiles -contains $newName) {
            $newName = "{0}({1}){2}" -f $file.BaseName, $count++, $file.Extension
        }
        # use Join-Path to create a FullName for the file
        $newFile = Join-Path -Path $DestinationFolder -ChildPath $newName
        Write-Verbose "Copying '$($file.FullName)' as '$newFile'"
        $file | Copy-Item -Destination $newFile -Force
    }
}
# you can change the folder paths, file pattern to filter etc. here
$destFolder = Join-Path -Path 'C:\Destination' -ChildPath ('{0:yyyy-MM-dd_HH-mm}' -f (Get-Date))
Copy-Unique -SourceFolder "C:\Origin" -DestinationFolder $destFolder -Filter '*.dat' -Recurse -NewerThanMinutes 300将代码更改为现在使用一个datetime对象来进行比较,而不是以分钟为单位进行比较。这也许会使代码更容易理解,但肯定会更灵活。
function Copy-Unique {
    # Copies files to a destination. If a file with the same name already exists in the destination,
    # the function will create a unique filename by appending '(x)' after the name, but before the extension. 
    # The 'x' is a numeric sequence value.
    [CmdletBinding(SupportsShouldProcess)]  # add support for -WhatIf switch
    Param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
        [Alias("Path")]
        [ValidateScript({Test-Path -Path $_ -PathType Container})]
        [string]$SourceFolder,
        [Parameter(Mandatory = $true, Position = 1)]
        [string]$DestinationFolder,
        [string]$Filter = '*',
        [datetime]$NewerThan = [datetime]::MinValue,
        [switch]$Recurse
    )
    # create the destination path if it does not exist
    if (!(Test-Path -Path $DestinationFolder -PathType Container)) {
        Write-Verbose "Creating folder '$DestinationFolder'"
        $null = New-Item -Path $DestinationFolder -ItemType 'Directory' -Force
    }
    # get a list of file FullNames in this source folder
    $sourceFiles = @(Get-ChildItem -Path $SourceFolder -Filter $Filter -File -Recurse:$Recurse)
    # if you want only files newer than a certain date, apply an extra filter
    if ($NewerThan -gt [datetime]::MinValue) {
        $sourceFiles = @($sourceFiles | Where-Object { $_.LastWriteTime -gt $NewerThan })
    }
    foreach ($file in $sourceFiles) {
        # get an array of all filenames (names only) of the files with a similar name already present in the destination folder
        $destFiles = @((Get-ChildItem $DestinationFolder -File -Filter "$($file.BaseName)*$($file.Extension)").Name)
        # for PowerShell version < 3.0 use this
        # $destFiles = @(Get-ChildItem $DestinationFolder -Filter "$baseName*$extension" | Where-Object { !($_.PSIsContainer) } | Select-Object -ExpandProperty Name)
        # construct the new filename
        $newName = $file.Name
        $count = 1
        while ($destFiles -contains $newName) {
            $newName = "{0}({1}){2}" -f $file.BaseName, $count++, $file.Extension
        }
        # use Join-Path to create a FullName for the file
        $newFile = Join-Path -Path $DestinationFolder -ChildPath $newName
        Write-Verbose "Copying '$($file.FullName)' as '$newFile'"
        $file | Copy-Item -Destination $newFile -Force
    }
}
# you can change the folder paths, file pattern to filter etc. here
$destFolder = Join-Path -Path 'D:\Destination' -ChildPath ('{0:yyyy-MM-dd_HH-mm}' -f (Get-Date))
Copy-Unique -SourceFolder "C:\Origin" -DestinationFolder $destFolder -Filter '*.dat' -Recurse -NewerThan (Get-Date).AddMinutes(-300)当您保存了上面的代码,让我们说'C:\Scripts\Copy-Unique.ps1‘之后,您可以从一个批处理文件中调用它,如下所示:
Powershell.exe -NoLogo -NoProfile -File "C:\Scripts\Copy-Unique.ps1"https://stackoverflow.com/questions/72043270
复制相似问题