[英]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. 我正在尝试编写Powershell脚本,该脚本将在应用程序存在的同一路径上创建backupfolder,并且需要在部署之前将文件夹和文件复制到backupfolder。 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
错误:无法将项目C:\\ XYZ \\ Backup_18-02-2017复制到自身上
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"
以下工作并显示字符串“ asdf”
$a_ = "adsf"; $a_
Your New-Item
cmdlet call should have failed since $source_
is not a variable and would return null. 您的
New-Item
cmdlet调用应该失败,因为$source_
不是变量,并且将返回null。 This is default behavior for PowerShell. 这是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. 因此,我希望您的文件夹变量为null。 wOxxOm brings this up in comment as well
wOxxOm也对此发表了评论
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 您仍然必须尝试从副本中排除该文件夹,以及Keith Hill的答案告诉您
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"
. 您想将
"C:\\XYZ"
并备份到名为"C:\\XYZ\\backup_$DATE"
。 What you will actually do is create a loop that will break once it reaches the max 248 characters. 您实际上要做的是创建一个循环,一旦达到最大248个字符,该循环就会中断。 If we use the
-exclude
option then we can exclude the backup directory "C:\\XYZ\\backup_$DATE"
. 如果使用
-exclude
选项,则可以排除备份目录"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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.