0

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

  1. 删除最旧的

    (file3.bak) --> none
    
  2. 增加备份目录中当前文件的文件名

        (file1.bak) --> (file2.bak)
        (file2.bak) --> (file3.bak)
    
  3. 将最新版本的文件从另一个目录复制到此备份目录

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

这是据我所知并且被卡住了:

$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-- }
4

2 回答 2

1

像这样的东西?删除 '-Whatif's 来做真实的事情。

$files = @(gci *.bak | sort @{e={$_.LastWriteTime}; asc=$true})

if ($files)
{
    del $files[0] -Whatif
    for ($i = 1; $i -lt $files.Count; ++$i)
     { ren $files[$i] $files[$i - 1] -Whatif }
}
于 2012-04-24T06:55:00.277 回答
1

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


#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-- }
于 2012-04-24T17:06:59.720 回答