[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
本帖最后由 ivor 于 2017-12-17 12:53 编辑

回复 1# wpy4206
powershell 脚本:
功能:以当前文件为基准,只移动深度三层目录下所有JPG图片至二层目录,然后删除空目录
  1. foreach ($dir in @(dir .\*\*\*))
  2. {
  3.     $count = 0
  4.     foreach ($file in @(dir $dir))
  5.     {
  6.         if ($file.Extension -eq ".jpg"){
  7.             if (! $file.PSIsContainer){
  8.                 if ($dir.Parent){
  9.                     $count += 1
  10.                     $new_file = -Join($dir.name,"(",$count,")",$file.Extension)
  11.                     $dest = -Join(".\",$dir.Parent.Parent,"\",$dir.Parent,"\",$new_file)
  12.                     $file.Moveto($dest)
  13.                     write-host 正在移动:$file
  14.                     if( (Get-ChildItem $dir | Measure-Object).Count -eq 0) {
  15.                         rd $dir
  16.                         write-host 删除目录:$dir
  17.                     }
  18.                 }
  19.             }
  20.         }
  21.     }
  22. }
复制代码
1

评分人数

#&cls&@powershell "Invoke-Expression ([Io.File]::ReadAllText('%~0',[Text.Encoding]::UTF8))" &pause&exit

TOP

本帖最后由 ivor 于 2017-12-17 16:53 编辑

回复 12# wpy4206

好吧,现在可以存为bat文件运行此代码了。
  1. <# :
  2. @echo off
  3. rem 增强代码兼容性,代码保存为 ".bat"、".cmd"、".ps1"均可执行
  4. powershell Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
  5. more +8 %~f0 > %~dpn0.ps1
  6. powershell %~dpn0.ps1
  7. del %~dpn0.ps1&pause&exit /b
  8. #>
  9. foreach ($dir in @(dir .\*\*\*))
  10. {
  11.     $count = 0
  12.     foreach ($file in @(dir $dir))
  13.     {
  14.         if ($file.Extension -eq ".jpg"){
  15.             if (! $file.PSIsContainer){
  16.                 if ($dir.Parent){
  17.                     $count += 1
  18.                     $new_file = -Join($dir.name,"(",$count,")",$file.Extension)
  19.                     $dest = -Join(".\",$dir.Parent.Parent,"\",$dir.Parent,"\",$new_file)
  20.                     $file.Moveto($dest)
  21.                     write-host 正在移动:$file
  22.                     if( (Get-ChildItem $dir | Measure-Object).Count -eq 0) {
  23.                         rd $dir
  24.                         write-host 删除目录:$dir
  25.                     }
  26.                 }
  27.             }
  28.         }
  29.     }
  30. }
复制代码
1

评分人数

#&cls&@powershell "Invoke-Expression ([Io.File]::ReadAllText('%~0',[Text.Encoding]::UTF8))" &pause&exit

TOP

返回列表