【问题标题】:Need to remove date string on file需要删除文件中的日期字符串
【发布时间】:2017-03-15 01:16:31
【问题描述】:

需要一些帮助来创建一个脚本来删除日期字符串和在 SQL Server 进行备份时添加的“备份”。

格式为:DatabaseName_backup_2016_10_24_010000_6215942

开始于:

Get-ChildItem -path C:\temp\Backup\ -include *.bak

将开始研究选项,同时寻找洞察力。开放使用其他脚本。

谢谢

【问题讨论】:

    标签: shell powershell command


    【解决方案1】:

    在玩了这个工作之后。 -whatif 开关需要被移除才能工作。我重命名了文件夹中的所有 SQL 备份:

    gci C:\SQL_backup\*.bak | ren -N { $_ -replace '_backup_\d+_\d+_\d+_\d+_\d+' } -whatif
    

    【讨论】:

    • 抱歉耽搁了。我是现场唯一的系统管理员
    【解决方案2】:
    gci c:\temp\backup\*.bak | ren -N { $_ -replace 'backup_\d+_\d+_\d+_' } -whatif
    # get matching files and | rename them with new names
    # replacement pattern is 'backup' followed by strings of digits and underscores, replaced with nothing.
    

    -whatif 表示它实际上不会做任何改变,只是告诉你它会做什么。


    帮助链接(如果有):

    • gci 是 Get-ChildItem 的别名(在 Microsoft.PowerShell.Management 模块中)
    • ren 是 Rename-Item 的别名(在 Microsoft.PowerShell.Management 模块中)

    【讨论】:

    • 我正要进行编辑以扩展别名……但这会破坏你的完美设计。
    【解决方案3】:

    你可以用正则表达式举例:

    $old = "DatabaseName_backup_2016_10_24_010000_6215942.bak"
    
    $old -replace "([^_]+_)(backup_\d+_\d+_\d+_)(\d+_\d+\.bak)","`$1`$3"
    
    #Result : DatabaseName_010000_6215942.bak
    

    更多信息在这里:https://powershell.org/2013/08/29/regular-expressions-are-a-replaces-best-friend/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-29
      • 2017-03-01
      • 1970-01-01
      • 2011-12-24
      • 1970-01-01
      • 2014-09-24
      相关资源
      最近更新 更多