Search code examples
vbscriptcommandimagemagick-convert

Run convert from VBScript on all files in a folder


Im trying to call this external command to convert every file in a directory from VBScript:

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

When im running below script on Windows 10 64-bit nothing happens. I already checked variables. It looks like I'm doing something wrong here, but I have no idea what.

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

Solution

  • Here, I took a stab at it. Seems you've got a lot of excessive work for nothing. A lot of strings combining empty strings and characters... See below. Test it out making any syntax adjustments and you're all set.

    The shell command execution will run as this in the current formatting....

    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"
    

    Here's the 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