简体   繁体   中英

Copy Files on Same Directory using Powershell

I am trying to write powershell Script which will create backupfolder on same Path where Application exist and need to copy the folders & files into backupfolder before deploying. Below are the command was using to perform but am getting error

$Source = "C:\XYZ"
$BackupFolder = New-Item -ItemType Directory -Force -Path $source_$(Get-Date) 

Copy-Item -Path $Source\* $BackupFolder -Force

Error: Cannot copy item C:\\XYZ\\Backup_18-02-2017 on to itself

Try:

Copy-Item $Source\* $BackupFolder -Exclude $BackupFolder

That will eliminate the folder that you are copying into as a source that is being copied from.

Variables can contain underscores. The following works and displays the string "asdf"

$a_ = "adsf"; $a_

Your New-Item cmdlet call should have failed since $source_ is not a variable and would return null. This is default behavior for PowerShell. When I run your code as is I get the following:

New-Item : Cannot find drive. A drive with the name '02/18/2017 22' does not exist.At line:1 char:1
+ New-Item -ItemType Directory -Force -Path "$source_$(Get-Date)" -what ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (02/18/2017 22:String) [New-Item], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.NewItemCommand

So I would have expected your folder variable to be null. wOxxOm brings this up in comment as well

Several options to address what I am sure is the partial source of your issue.

$BackupFolder = New-Item -ItemType Directory -Force -Path "$source`_$(Get-Date)" 
$BackupFolder = New-Item -ItemType Directory -Force -Path "$($source)_$(Get-Date)" 
$BackupFolder = New-Item -ItemType Directory -Force -Path ("{0}_{1} -f "$source, Get-Date) 

You will still have to try and exclude this folder from the copy as well like Keith Hill's answer is telling you

 Copy-Item $Source\\* $BackupFolder -Exclude $BackupFolder 

try Something like this

 $Source = "C:\XYZ"
 $Destination="{0}{1:yyyyMMdd}" -f $source, (Get-Date)
 New-Item -ItemType Directory -Force -Path $Destination

 Copy-Item -Path $Source\* $Destination -Recurse -Force

If I understand the question correctly. You want to take "C:\\XYZ" and backup into the same directory called "C:\\XYZ\\backup_$DATE" . What you will actually do is create a loop that will break once it reaches the max 248 characters. If we use the -exclude option then we can exclude the backup directory "C:\\XYZ\\backup_$DATE" .

This function will do the trick and also gives you error handling.

Function Get-CopyDirectory{
#####################
# Dynamic Variables #
#####################

$Date = Get-Date -format ddMM-yyyy
$Exclude="Backup*"

####################
# Static Variables #
####################
$AppPath = "F:\Test\"
$BackupPath = "$AppPath\BACKUP_$Date\"

if (Test-Path $BackupPath) {
Write-Host "Backup Exist" -f Cyan
}
else 
{
Copy-Item "$AppPath\*" $BackupPath -Exclude $Exclude -recurse -verbose
}
}
CLS
Get-CopyDirectory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM