好久没有写过了,
PS 3.0 以上,否则勿试。- function Rename-File {
- param([string]$dir, [string]$base, [string]$ext,
- [string]$bn = '', [uint16]$n = 0);
-
- if ([IO.File]::Exists("$dir\$($base+$bn+$ext)")) {
- $bn = ' (' + ++$n + ')';
- return Rename-File -dir $dir -base $base -bn $bn -ext $ext -n $n;
- }
- return $base + $bn;
- }
-
- ls 'D:\photos\*' -Include '*.jpg', '*.png' -file | % {
- $f_str = $_.BaseName.Substring(0, 1);
- $dir = "$pwd\images\$f_str";
- if (![IO.Directory]::Exists($dir)) { md $dir | Out-Null }
-
- $base = Rename-File -dir $dir -base $_.BaseName -ext $_.Extension;
- cp $_.FullName -dest "$dir\$($base+$_.Extension)" -Verbose;
- # cp 改为 move 则移动文件。
- }
- Pause
复制代码
|