简体   繁体   English

使用Powershell在同一目录中复制文件

[英]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.

相关问题 使用Powershell仅复制文件而不复制目录结构 - copy files only not directory structure using powershell 使用PowerShell从特定目录复制文件 - Copy files from specific directory using PowerShell 使用PowerShell复制源服务器相同目录结构中的文件夹和子文件夹中的项目文件 - Copy-item Files in Folders and subfolders in the same directory structure of source server using PowerShell 使用Powershell复制目录(和子目录)而不覆盖/删除文件 - using Powershell to copy a directory (and subdirectory(s)) withouth overwriting/removing files 我无法使用Powershell在Windows目录内复制文件 - I am not able to copy files inside windows directory using powershell 使用 Powershell 复制文件 - Copy files using Powershell 将文件从 tfs 版本控制复制到带有 PowerShell 的目录 - Copy files from tfs versioncontrol to directory with PowerShell Powershell 仅从目录中复制匹配的文件 - Powershell Only Copy Matching Files from Directory 使用PowerShell将前N个文件从源目录复制到“序列化”目标目录 - Copy first N files from source directory to “serialized” destination directory using powershell Powershell:根据目录名称查找文件,复制并重命名到同一文件夹中并保持原始 - Powershell: find files based on directory name, copy and rename into same folder and keep original
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM