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 に答える