在文件夹中的所有文件上运行VBScript转换

时间:2016-12-10 00:02:49

标签: vbscript command imagemagick-convert

我试图调用此外部命令来转换目录中的每个文件来自VBScript:

convert -type bilevel -compress group4 -pointsize 20 -draw "text 10,20 'PropertyValue1 PropertyValue2 PropertyVAlue3 PropertyValue4 PropertyValue5' " input.tif output.tif

当我在Windows 10 64位以下的脚本下运行时没有任何反应。我已经检查了变量。看起来我在这里做错了,但我不知道是什么。

的VBScript:

strFolder1 = "C:\Users\Kamil\Desktop\Nowyfolder\PDF TO TIFF imagemagick\tify" 'path to folder with .tif files ready to convert to readable tif

Dim aFileNameSize(0, 0)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colFolder = objFSO.GetFolder(strFolder1)
Set colFiles = colFolder.Files

Dim list
Set list = CreateObject("System.Collections.ArrayList")

For Each strFile In colFiles
  aFileNameSize(0,0) = strfile.Name
  list.Add strfile.name
Next

For i = 0 To list.Count
  Do:
    If i = list.Count Then Exit Do

    Dim x
    a = "C:\Users\Kamil\Desktop\Nowyfolder\PDF TO TIFF imagemagick\tify "
    d = """text 10,20 'PropertyValue1 PropertyValue2 PropertyVAlue3 PropertyValue4 PropertyValue5"
    f = "'"
    g = "" & Chr(34) & ""
    h = "" & d & "" & "" & f & "" & "" & Chr(32) & "" & "" & g & ""
    b = "convert -type bilevel -compress group4 -pointsize 20 -draw " & d & "" & "" & f & "" & "" & Chr(32) & "" & "" & g &  ""
    c = "" & list.Item(i) & ""
    x = "" & a & "" & "" & b & "" & "" & list.Item(i) & "" & Chr(32) & "" & "" & c & ""

    Set oShell = WScript.CreateObject("WSCript.Shell")
    oShell.Run "cmd cd """ & x & """"
  Loop While False
Next

2 个答案:

答案 0 :(得分:0)

我发布固定代码:

strFolder1 = "C:\Users\Kamil\Desktop\Nowyfolder\PDF TO TIFF imagemagick\tify" 'path to folder with .tif files ready to convert to readable tif

Dim aFileNameSize(0, 0)

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set colFolder = objFSO.GetFolder(strFolder1)

Set colFiles = colFolder.Files

dim list
Set list = CreateObject("System.Collections.ArrayList")


For Each strFile In colFiles

aFileNameSize(0,0) = strfile.name

list.Add strfile.name

Next


For i = 0 to list.Count -1
Do:
 If i = list.Count Then Exit Do


Dim x
a = "C:\Users\Kamil\Desktop\Nowyfolder\PDF TO TIFF imagemagick\tify "

d = """text 10,20 'PropertyValue1 PropertyValue2 PropertyVAlue3 PropertyValue4 PropertyValue5"

f = "'"

g = "" & chr(34) & ""


h = "" & d & "" & "" & f & "" & "" & chr(32) & "" & "" & g & ""


n = """" & strFolder1 & """"


b = "convert -type bilevel -compress group4 -pointsize 20 -draw " & d & "" & "" & f & "" & "" & chr(32) & "" & "" & g &""


c = "" & list.item(i) & ""


x = "" & b & "" & "" & chr(32) & "" & "" & list.item(i) & "" & chr(32) & "" & "" & c & ""





Set objShell = WScript.CreateObject ("WScript.Shell")
objShell.run "cmd /K CD " & n & " & " & x & "", 0, True




Loop While False
Next

答案 1 :(得分:0)

在这里,我捅了一下。看起来你已经做了很多过分的工作。很多字符串组合了空字符串和字符......见下文。测试它进行任何语法调整,你就完成了。

shell命令执行将在当前格式化中运行....

cmd.exe /c "C:\Users\Kamil\Desktop\Nowyfolder\PDF TO TIFF imagemagick\tify\convert.exe -type bilevel -compress Group4 -pointsize 20 -draw "text 10,20 \'PropertyValue1 PropertyValue2 PropertyVAlue3 PropertyValue4 PropertyValue5\'" somefile.tif"

这是VBS

strfldr = "C:\Users\Kamil\Desktop\Nowyfolder\PDF TO TIFF imagemagick\tify"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colFolder = objFSO.GetFolder(strfldr)
Set colFiles = colFolder.Files
'unnecessary array removed

For Each objfile In colFiles 'Properly notate file as Object, not string
    'if instr(1, objFile.Name, ".tif", 1) <> 0 Then ShellRun(ConvertFile(objFile.Name))
    if instr(1, objFile.Name, ".tif", 1) <> 0 Then ShellExec(ConvertFile(objFile.Name))
Next

Function ConvertFile(strFileName)
    Exec = "C:\Users\Kamil\Desktop\Nowyfolder\PDF TO TIFF imagemagick\tify\convert.exe"
    TypeParam = "-type bilevel"
    CompParam = "-compress Group4"
    PsizeParam = "-pointsize 20"
    DrawParam = "-draw ""text 10,20 \'PropertyValue1 PropertyValue2 PropertyVAlue3 PropertyValue4 PropertyValue5\'"""
    Params = TypeParam & " " & CompParam & " " & PsizeParam & " " & DrawParam & " " & strFileName & " " & strFileName
    ConvertFile = """" & Exec & " " & Params & """"
    'Returns properly formatted Command string
End Function

Function ShellRun(strCommand)
    On Error Resume Next
    if NOT isObject(shell) then Set shell = CreateObject("Wscript.Shell")
    wscript.echo strcommand
    shell.run "cmd.exe /c " & strCommand, 0, true
    If Err.Number <> 0 Then
        msgbox "Error Code:" & err.Number & vbCrLf & "Error Description: " & Err.Description, 0, "Shell Command Error " & err.number & "!"
        err.clear
    End if
    on error goto 0
    Set shell = nothing
    'Executes command shell hidden - returns error code via msgbox.
End Function

Sub ShellExec(strCommand)
    ShellExecReturn = CreateObject("Wscript.Shell").Exec("cmd /c " & strCommand ).stdout.readall
    wscript.sleep 1000
    wscript.echo ShellExecReturn
End Sub