PowerShell code samples

[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;
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值