Gatewarden
Gatewarden

Reputation: 393

Batch Replace-Name & Move-item

I got a script:

$CopySource = "C:\Source\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"

$CopyDestination = "C:\Dest\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"

$files = Get-ChildItem -File "*.qvd" $CopySource -Force

foreach ($file in $files) {

Copy-Item -path $CopySource\$_$file -Destination $CopyDestination -Force

}

$CopySource = "C:\Source\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"

$CopyDestination = "C:\Dest\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"

$items = Get-ChildItem -File "*.qvd" $CopyDestination

foreach($I in $Items) {

$newfilename= rename-item -path $I.Name -newname ("Agresso_" + $I.Name)

The problem I have is that I need to copy files from source to destination and once they are over they need to have agresso_ added. Then they day after a batch of files will be copied again and also they need to be renamed to agresso_ and overwrite the old ones, preferably with move-item. I got a problem already, as I am not used with prefixes,

I tried enless of versions with where-object and simular, could not figure out a way to use test-path either.

I did exactly this but with renaming the files, like this for maximo:

$files = Get-ChildItem -File "*.qvd" $CopySource -Force

foreach ($file in $files) {

Copy-Item -path $CopySource\$_$file -Destination $CopyDestination -Force -Verbose 4>&1 |

Out-File -Append $logpath

}

"Klar med Kopiering av .qvd filer $global:currenttime" | Out-File $logpath -Append

"Påbörjar omdöpning av .qvd filer $global:currenttime" | Out-File $logpath -Append

$items = Get-ChildItem -File "*.qvd" $CopyDestination

foreach($I in $Items) {

$newfilename=$I.Name.Replace("QVDLager1","Maximo")

Move-Item -Path $I.FullName -Destination $CopyDestination\$newfilename -Force -Verbose 4>&1 |

Out-File -Append $logpath

If anyone can help me in the right direction it would be highly appriciated.

Upvotes: 0

Views: 47

Answers (1)

Theo
Theo

Reputation: 61068

You can copy (or move) and rename the destination at the same time:

$CopySource      = "C:\Source\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"
$CopyDestination = "C:\Dest\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Agresso"

Get-ChildItem -Path $CopySource -File "*.qvd" -Force | ForEach-Object {
    # create the full path for the target file with "Agresso_" prefixed
    $target = Join-Path -Path $CopyDestination -ChildPath ('Agresso_{0}' -f $_.Name)
    $_ | Copy-Item -Destination $target -WhatIf
}

If satisfied with the results of the code shown in the console, you can remove the -WhatIf switch to really start copying (or moving) the files.

Upvotes: 1

Related Questions