[2010-12-30T13:51:57+08:00] get-service | where-object {$_.DisplayName -match "^w.*"}
[2011-01-12T22:42:59+08:00] powershell+du: (du -v -l 2 d: | sort -desc)[3..20]
[2011-01-24T21:29:05+08:00] powershell Tee-Object
Send the input object(s) to two places, an input object is piped to a file or variable, and then also passed along the pipeline.
==Rename all files in a folder==
$i=1;
foreach($f in dir) {
$old = $f.Name;
$new = $old -replace "^\w+",$i;
$i++;
$old = ".\" + $old
if($new -ne $old){
echo $old;
echo $new;
ren $old $new
}
}
==Bulk File Rename==
作用:批量给wav文件加文件夹前缀,把指定文件夹下的所有一级文件夹下的所有wav文件加上 "文件夹名_" 前缀
用法:
1. windows开始菜单打开powershell ISE,复制脚本到最上面窗口,按F5
或者
2. 保存成 .ps1 文件,右键选运行为powershell
重命名:
$path = Read-Host "根文件夹路径(比如: E:\ 或者 D:\Music ) "
pushd $path;
dir |
foreach{
$type = $_.GetType();
if($type -match "DirectoryInfo"){
$subFolder = $_.Name;
$subFolderFiles = ".\" + $subFolder + "\*.wav";
foreach ( $1 in dir $subFolderFiles){
$oldName = ".\" + $_.Name + "\" + $1.Name;
$newName = $subFolder+"_"+$1.Name;
ren $oldName $newName
}
}
}
复原:
$path = Read-Host "根文件夹路径(比如: E:\ 或者 D:\Music ) "
pushd $path;
dir |
foreach{
$type = $_.GetType();
if($type -match "DirectoryInfo"){
$subFolder = $_.Name;
$subFolderFiles = ".\" + $subFolder + "\*.wav";
foreach ( $1 in dir $subFolderFiles){
$oldName = ".\" + $_.Name + "\" + $1.Name;
$pattern = "^(" + $subFolder + "_)+";
$newName = $1.Name -replace $pattern, "";
ren $oldName $newName
}
}
}
==Bulk File Rename & Create .m3u==
$path = Read-Host "根文件夹路径(比如: E:\ 或者 D:\Music ) "
pushd $path;
dir |
foreach{
$type = $_.GetType();
if($type -match "DirectoryInfo"){
$subFolder = $_.Name;
$subFolderFiles = ".\" + $subFolder + "\*.mp3";
foreach ( $1 in (dir $subFolderFiles | sort Name ) ){
$fileName = $1.Name;
if(! $fileName.StartsWith($subFolder)){
$oldName = ".\" + $_.Name + "\" + $fileName;
$newName = $subFolder+"_"+$fileName;
$newName = $newName -replace '[^-_.\da-zA-Z]',''
ren $oldName $newName
$fileName = $newName;
}
$subFolderM3uFile = $subFolder + '.m3u';
$m3uFile = $subFolder+'\'+ $fileName;
echo $m3uFile >> $subFolderM3uFile;
}
}
}