2013-05-13 52 views
-2

我正在项目中部署工具自动添加“.update”扩展名,如果目标上已存在相同的文件。 例如Powershell脚本来备份和替换文件

root 
    web.config 
    web.config.update 
    connection.config 
    connection.config.update 

我想执行下列后通过部署的PowerShell:

  1. 备份的* .config。
  2. 用* .update文件替换现有的* .config。 以下是所希望的输出中:

    根 的web.config web.config.update CONNECTION.CONFIG connection.config.update 根 的web.config CONNECTION.CONFIG 备份 的web.config 连接.config

请问有人可以通过使用powershell来实现上述目标吗?

+0

,还有什么疑问或问题? – Solaflex 2013-05-13 07:02:27

回答

4

以下代码将执行您想要的操作。

  1. 将现有配置文件备份到基于当前日期的 命名的备份文件夹中。
  2. 通过删除它们并重命名更新文件来替换现有的配置文件。

    $root_folder = 'c:\temp\root' 
    
    # Create a backup folder 
    $backup_directory = New-Item -Path "$root_folder\backup_$(Get-Date -Format yyyyMMdd)" -Force -ItemType Directory 
    
    Get-ChildItem -Filter *.config | ForEach-Object { 
    
        # Copy .config files to the backup directory 
        Copy-Item -Path $_.FullName -Destination "$($backup_directory.Fullname)" -Force 
    
        # Delete the file from the source directory 
        $_ | Remove-Item 
    
        # Rename the .update files to .config files. 
        Rename-Item -Path "$($_.FullName).update" -NewName $_.FullName -Force 
    } 
    
+1

我想知道为什么你的问题被拒绝。我想你可以重新说明它,使其更好: 1.我有以下要求等 2.我如何使用Powershell实现此目的? – MFT 2013-05-13 08:49:21

+0

谢谢,我的错我错过了我的复制粘贴从记事本+ +的最后一行 – 2013-05-13 12:21:33