RSS

rename-item的高级应用

13 Oct

1.替换文件名中的指定字符串:

Get-ChildItem C:\Users\shichng\Desktop\test2 -Force -Recurse  | rename-item -newname { $_.name -replace ‘microsoft’,” } –force

将所有文件名中的microsoft删除

2.批量修改文件后缀名
Set-Location C:\Users\shichng\Desktop\test
$a=Get-ChildItem -Force -Recurse C:\Users\shichng\Desktop\test
foreach($b in $a)
{ $c=$b.name+".txt"
Rename-Item $b -NewName $c -Force
}

将所有文件的扩展名强制更改为.txt

3.批量删除文件名中的空格
$a=Get-ChildItem -Force -Recurse C:\Users\shichng\Desktop\test -Include *.txt
foreach($b in $a)
{
$C=$b.name
$d=$c.TrimStart(" ")
rename-item  $b -newname  $d  -force
}

删除文件名中的空格

4.重命名所有文件
$a=Get-ChildItem -Force -Recurse C:\Users\shichng\Desktop\test -Include *.txt
foreach($b in $a)
{
$c=get-content $b -totalcount 1
$d=$c+”.txt”
rename-item  $b -newname  $d  -force
}

所有文件使用正文的第一行作为标题,并将扩展名设置为.txt

 

本文来自http://orca2007.spaces.live.com/blog/cns!330865806FCE202!6303.entry?sa=483951566

Advertisements
 
Leave a comment

Posted by on October 13, 2009 in Powershell

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: