Powershell 脚本文件名增量 [英] Powershell Script Filename Increment

查看:44
本文介绍了Powershell 脚本文件名增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Powershell 中,我想自动化更改一组文件的文件名并将类似文件的最新版本复制到该目录的过程.

  1. 删除最旧的

    (file3.bak) -->没有任何

  2. 增加备份目录中当前文件的文件名

     (file1.bak) -->(file2.bak)(file2.bak) -->(file3.bak)

  3. 从另一个目录复制最新版本的文件到这个备份目录

    (newestfile.txt) -->(file1.bak)

这是我得到的并且卡住了:

$path = "c:\temp"cd $路径$count = (get-childitem $path -name).count写主机文件数:$count"$items = Get-ChildItem |排序扩展 -desc |Rename-Item -NewName {"gapr.ear.rollback$count"}$items |排序扩展 -desc |ForEach-Object -begin { $count= (get-childitem $path -name).count } -process { rename-item $_ -NewName "gappr.ear.rollback$count";$count-- }

解决方案

感谢所有回复的人.感谢您的帮助

<预><代码>#完成脚本的目录$path = "c:\temp"cd $路径#将目录中的文件数写出到控制台$count = (get-childitem $path -name).count写主机文件数:$count"#按降序排列项目$items = Get-ChildItem |排序扩展 -desc#按文件扩展名删除最旧的文件删除 $items[0]#将文件从原目录复制到备份目录复制项目 c:\temp2\* c:\temp#按降序排列项目$items = Get-ChildItem |排序扩展 -desc#在 NewName 参数后用引号重命名文件$items |ForEach-Object -begin { $count= (get-childitem $path -name).count } -process { rename-item $_ -NewName "file.bak$count";$count-- }

Within Powershell I want to automate the process of changing the file name of a group of files and copy the latest version of a similar file to that directory.

  1. Delete oldest

    (file3.bak) --> none
    

  2. Increase filename of current files in backup directory

        (file1.bak) --> (file2.bak)
        (file2.bak) --> (file3.bak)
    

  3. Copy latest version of file from another directory to this backup directory

    (newestfile.txt)   --> (file1.bak)
    

This is as far as I have gotten and am stuck:

$path = "c:\temp"
cd $path

$count = (get-childitem $path -name).count
Write-Host "Number of Files: $count"

$items = Get-ChildItem | Sort Extension -desc | Rename-Item -NewName {"gapr.ear.rollback$count"}

$items | Sort Extension -desc | ForEach-Object  -begin { $count= (get-childitem $path -name).count }  -process { rename-item $_ -NewName "gappr.ear.rollback$count"; $count-- }

解决方案

Thanks to all who responded. Your help was appreciated


#Directory to complete script in
$path = "c:\temp"
cd $path

#Writes out number of files in directory to console
$count = (get-childitem $path -name).count
Write-Host "Number of Files: $count"

#Sorts items by decsending order
$items = Get-ChildItem | Sort Extension -desc 

#Deletes oldest file by file extension number
del $items[0]

#Copy file from original directory to backup directory
Copy-Item c:\temp2\* c:\temp

#Sorts items by decsending order
$items = Get-ChildItem | Sort Extension -desc

#Renames files in quotes after NewName argument
$items | ForEach-Object  -begin { $count= (get-childitem $path -name).count }  -process { rename-item $_ -NewName "file.bak$count"; $count-- }

这篇关于Powershell 脚本文件名增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆