Board logo

标题: [文件操作] 求助BAT,按垂直分辨率把文件夹内图片复制移动 [打印本页]

作者: ckddd1266    时间: 2019-11-22 18:38     标题: 求助BAT,按垂直分辨率把文件夹内图片复制移动

图片类型都是.tif 或tiff

需求把文件夹包含子文件夹下所有tiff图片,

这些图片有 300dpi 有 200dpi  有150dpi 的,具体制定一种,复制移动到另一个地方。

不胜感激
作者: terse    时间: 2019-11-22 20:27

  1. $ph =  "D:\Image"
  2. $Directory = "D:\Image\300"
  3. if (![IO.Directory]::Exists("$Directory"))  
  4. {
  5.     $null = New-Item $Directory  -type directory
  6. }
  7. $Files = @()
  8. $extlists = @('.tif','.ttif')
  9. Foreach ( $ext in $extlists )
  10. {
  11.      Foreach ($File in [IO.Directory]::EnumerateFiles("$ph","*$ext","TopDirectoryOnly"))
  12.      {
  13.            $img = [System.Drawing.Image]::FromFile($File)
  14.            $dpiX = $img.HorizontalResolution
  15.            $img.Dispose()
  16.            if ($dpiX -eq 300) {
  17.                $Files += $File
  18.            }
  19.      }
  20. }
  21. $Files | Move-Item -Destination $Directory
复制代码

作者: zaqmlp    时间: 2019-11-22 22:51

本帖最后由 zaqmlp 于 2019-11-23 18:43 编辑
  1. /*&cls
  2. @echo off
  3. set info=互助互利,支付宝扫码头像,感谢打赏
  4. rem 有问题,可加QQ956535081及时沟通
  5. title %info%
  6. set "oldfolder=D:\xxx\源目录"
  7. set "newfolder=D:\xxx\新目录"
  8. set vdpi=300
  9. if not exist "%oldfolder%\" (echo;"%oldfolder%" not found&goto end)
  10. if not exist "%newfolder%\" md "%newfolder%\"
  11. cd /d "%oldfolder%\"
  12. for /f "delims=" %%a in ('dir /a-d/b/s *.tif^|cscript -nologo -e:jscript "%~f0" %vdpi%') do (
  13.     echo;"%%a"
  14.     move /y "%%a" "%newfolder%\"
  15. )
  16. :end
  17. echo;%info%
  18. pause
  19. exit
  20. */
  21. var fso=new ActiveXObject('Scripting.FileSystemObject');
  22. var sa=new ActiveXObject('Shell.Application');
  23. var v=0,objFolder=sa.NameSpace(0);
  24. for(var i=0; i<350; i++){if(/^垂直分[辨辩]率/.test(objFolder.GetDetailsOf(null, i))){v=i;}}
  25. while(!WSH.StdIn.AtEndOfStream){
  26.     var f=fso.GetFile(WSH.StdIn.ReadLine());
  27.     var objFolder=sa.Namespace(f.ParentFolder.Path);
  28.     var objItem=objFolder.ParseName(f.Name);
  29.     var vdpi=objFolder.GetDetailsOf(objItem, v).replace(/\D/g,'');
  30.     if(vdpi==Number(WSH.Arguments(0))){WSH.echo(f.Path);}
  31. }
复制代码

作者: ckddd1266    时间: 2019-11-22 23:01

回复 2# terse


请大佬再指点下,因为完全看不懂。直接建了个样例,没有搬运成功。
作者: ckddd1266    时间: 2019-11-22 23:10

回复 3# zaqmlp
作者: terse    时间: 2019-11-22 23:11

回复 4# ckddd1266
不是BAT 是powershell 文件后缀是ps1
你什么系统 右键直接运行
作者: ckddd1266    时间: 2019-11-22 23:16

回复 6# terse


    2008r2,脚本可以运行了,但没有实现效果
作者: zaqmlp    时间: 2019-11-22 23:37

回复 5# ckddd1266

已改
作者: terse    时间: 2019-11-23 00:24

回复 7# ckddd1266
不确定什么因素 我这边测试可以的 你代码运行提示什么
作者: terse    时间: 2019-11-23 08:26

在D:\Image目录测试 看什么提示
  1. $Directory = "D:\Image\300"
  2. if (![System.IO.Directory]::Exists($Directory)) {$null = New-Item $Directory  -type directory}
  3. Add-Type -AssemblyName "System.Drawing"
  4. ls * -Include *.tif,*.ttif |?{$img=[Drawing.Image]::FromFile($_);$img.HorizontalResolution -eq 300; $img.Dispose()}| Move-Item -Destination $Directory
  5. "按任意键结束"
  6. $null = [Console]::Readkey()
复制代码
回复 7# ckddd1266
作者: ckddd1266    时间: 2019-11-23 11:37

回复 10# terse
你好,窗口闪现没有提示
作者: ckddd1266    时间: 2019-11-23 11:38

回复 8# zaqmlp


    你好,测试少量文件实现了,再去跑一个大概两万多文件的,半个多小时没有移动一个。
作者: flashercs    时间: 2019-11-23 12:32

本帖最后由 flashercs 于 2019-11-23 12:41 编辑

保存为 "按分辨率移动图片.bat" ,先设置路径及扩展名和分辨率,然后双击运行
  1. ' &cls&@cscript -nologo -e:vbscript %0 & pause & exit /b
  2. Option Explicit
  3. On Error Resume Next
  4. Dim srcDir ' As String
  5. Dim dstDir ' As String
  6. Dim arrPicExt ' As String()
  7. Dim HorizontalResolution 'As Double
  8. ' 源目录
  9. srcDir = "D:\image"
  10. ' 目标目录
  11. dstDir = "D:\image\300"
  12. ' 扩展名列表
  13. arrPicExt = Array("tif","tiff")
  14. ' 图片水平分辨率
  15. HorizontalResolution = 300
  16. Dim fso,wiaimage
  17. Set fso = CreateObject("Scripting.FileSystemObject")
  18. Set wiaimage = CreateObject("WIA.ImageFile")
  19. ShowError
  20. If Not fso.FolderExists(dstDir) Then
  21.   fso.CreateFolder(dstDir)
  22. End If
  23. ShowError
  24. ProcessImage fso.GetFolder(srcDir)
  25. ShowError
  26. Set fso = Nothing
  27. Set wiaimage = Nothing
  28. Function Includes(ByRef arrIn,ByRef strExt)
  29.   Dim Element
  30.   For Each Element In arrIn
  31.     If Element = strExt Then
  32.       Includes = True
  33.       Exit Function      
  34.       End If
  35.     Next
  36.   Includes = False
  37. End Function
  38. Sub ProcessImage(objFolder)
  39.   Dim objFile
  40.   For Each objFile In objFolder.Files
  41.      If Includes(arrPicExt,LCase(fso.GetExtensionName(objFile.Name))) Then
  42.       wiaimage.LoadFile objFile.Path
  43.       ShowError
  44.       If wiaimage.HorizontalResolution = HorizontalResolution Then
  45.         WSH.Echo "移动 """ & objFile.Path & """ 到 """ & dstDir & "\"""
  46.         objFile.Move dstDir & "\"
  47.         ShowError
  48.       End If
  49.     End If
  50.   Next
  51. End Sub
  52. Sub ShowError()
  53.   If Err.Number <> 0 Then
  54.     WSH.Echo "Err # " & Err.Number & vbNewLine & _
  55.     "Description: " & Err.Description & vbnewline & _
  56.     "Source: " & Err.Source
  57.     Err.Clear
  58.   End If
  59. End Sub
复制代码

作者: terse    时间: 2019-11-23 14:07

回复 [url=http://www.bathome.net/redirect.php?
goto=findpost&pid=225144&ptid=54313]11#[/url] ckddd1266

没有提示错误的话 代码应该没问题 可能运行环境引起闪退吧




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2