English 中文(简体)
Powerhell 方言
原标题:Powershell Script Filename Increment
  • 时间:2012-04-23 20:28:11
  •  标签:
  • powershell

内部权力 我想使一组档案名称的变更过程自动化,并复制该目录上类似档案的最新版本。

  1. 删除

    (file3.bak) --> none
    
  2. 更新目录中现有档案的名称

        (file1.bak) --> (file2.bak)
        (file2.bak) --> (file3.bak)
    
  3. 另一名录的最新档案版本

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

这是我ten忙:的:

$path = "c:	emp"
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:	emp"
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:	emp2* c:	emp

#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-- }
问题回答

与此类似? Remove - Whatif s to do the real matter.

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




相关问题
Mutually exclusive powershell parameters

SCENARIO I m writing a cmdlet for Powershell 2.0 using Visual Studio 2008 and .NET 3.5 the cmdlet requires 3 arguments. my intended grammar of the cmdlet is something like this: cmdletname [foo|...

Run a program from PowerShell with timeout

I ll write a script that runs a program and wait for it finished. But if the program is not finished within a specified time I want that the program is killed.

How to transpose data in powershell

I have a file that looks like this: a,1 b,2 c,3 a,4 b,5 c,6 (...repeat 1,000s of lines) How can I transpose it into this? a,b,c 1,2,3 4,5,6 Thanks

Powershell v2 remoting and delegation

I have installed Powershell V2 on 2 machines and run Enable-PsRemoting on both of them. Both machines are Win 2003 R2 and are joined to the same active directory domain and I can successfully run ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

热门标签