-3

how can I transform this innocent cmd batch code file to work in a ".vbs" file?

@echo off 
setlocal
for %%I in (*.pdf) do (
md "%%~nI"
"C:\GS\gswin32c.exe" -dNOPAUSE -dBATCH -sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4  -dJPEGQ=100  -r600 -sOutputFile="%%~nI\p%%02d.jpeg" "%%~I")

I got this far, I don't know how to write the GS script parameters

dim sFolder,MyArray 
dim FSO,OutPutFile
dim networkInfo
Dim objShell

sFolder = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set networkInfo = CreateObject("WScript.NetWork") 
Set objShell = WScript.CreateObject( "WScript.Shell" )  

For Each oFile In oFSO.GetFolder(sFolder).Files
  If Instr( 1, oFile.Name, ".pdf", vbTextCompare )>0 Then


            dirname =  Replace(oFile.Name,"." & oFSO.GetExtensionName(oFile.Path),"") 

            on error resume next
                oFSO.CreateFolder  dirname
            on error goto 0 


            
            objShell.Run "C:\GS\gswin32c.exe"  '/////// here help! 
            Set objShell = Nothing

            
  End if
Next

Set oFSO = Nothing
  • 1
    There's nothing wrong with using VBScript to run a command line tool. Maybe this is part of a much larger script or is being rolled into an HTA. Whatever. The only issue is that the question of how to get the command line correct has been asked on SO many times before (expect a duplicate link soon). The issue is easily avoided by prototyping your `Run` command using `MsgBox`. Don't try to run the command until you get the command string correct! – LesFerch Apr 20 '22 at 13:07
  • Does this answer your question? [Can't get WSCript.Shell object's Run method to work](https://stackoverflow.com/questions/63964923/cant-get-wscript-shell-objects-run-method-to-work) – LesFerch Apr 20 '22 at 13:14

1 Answers1

0

well, I finished the code, works as intended In the end, I generated a ".bat" and run it because if processed as an individual "objShell.Run" it opened for every pdf a separate cmd window, which was not acceptable.

thanks all for the hints. :)

dim sFolder,MyArray 
dim FSO,OutPutFile
dim networkInfo
Dim objShell
dim commmand,commmand2
dim ffso, crit
Dim intAnswer
dim nn

sFolder = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set networkInfo = CreateObject("WScript.NetWork") 
Set objShell = WScript.CreateObject("WScript.Shell")    
Set ffso = CreateObject("Scripting.FileSystemObject")

tmpfile=ffso.getspecialfolder(2) & "\" & ffso.gettempname
tmpfile=tmpfile & ".bat"  

set ffso=createobject("scripting.filesystemobject")
set ots=ffso.opentextfile(tmpfile,2,true)

ots.writeline "@echo off"
ots.writeline "setlocal"


        intAnswer = _
            Msgbox("Procesare doar cu Rutare?" & vbNewLine  & "Yes - doar cele cu #"& vbNewLine  & "No - toate fisierele", _
                vbYesNo, "Selectie ")
        If intAnswer = vbYes Then
            crit = "#.pdf"
        Else
            crit = ".pdf"
        End If

nn = 0


For Each oFile In oFSO.GetFolder(sFolder).Files
  If Instr( 1, oFile.Name, crit, vbTextCompare )>0 Then
            
            nn=nn+1

            dirname =  Replace(oFile.Name,"." & oFSO.GetExtensionName(oFile.Path),"") 

            on error resume next
                oFSO.CreateFolder  dirname
            on error goto 0 

                if Instr( 1, oFile.Name, "#.pdf", vbTextCompare )>0 then
                MyArray = Split(oFile.Name, "#", -1, 1)
                rutare = MyArray(UBound(MyArray)-1)
                else
                rutare = Replace(oFile.Name,"." & oFSO.GetExtensionName(oFile.Path),"") 
                end if
                
                
                
                
            Set FSO = CreateObject("Scripting.FileSystemObject")
            Set OutPutFile = FSO.OpenTextFile( sFolder & "\" & dirname & "\" & rutare & ".txt"  ,8 , True) 
            OutPutFile.WriteLine(FormatDateTime(Now, vbGeneralDate) & vbtab &  networkInfo.UserName & vbtab &   networkInfo.ComputerName)
            OutPutFile.close
            Set FSO= Nothing

                


commmand = "C:\GS\gswin32c.exe   -dNOPAUSE -dBATCH -sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4  -dJPEGQ=100  -r600 -sOutputFile=" & chr(34) & sFolder & "\" & dirname  & "\p%%02d.jpeg" & chr(34) & " " & chr(34) & sFolder & "\" & oFile.Name & chr(34)


ots.writeline   "@echo -----------------------------------------------------------------------------"
ots.writeline   "@echo Procesare - " &  oFile.Name 
ots.writeline   "@echo -----------------------------------------------------------------------------"
ots.writeline commmand

        
  End if
Next




if nn=0 then
        ots.writeline   "@echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        ots.writeline   "@echo  Nu exista PDF uri sau cu extensia " & crit
        ots.writeline   "@echo  Pentru toate PDF-urile se selecteaza No!"
        ots.writeline   "@echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
end if



ots.writeline "timeout 6"
objShell.run "%comspec% /c " & tmpfile,1, True
ots.close


ffso.deletefile tmpfile,true 
set ffso=nothing
Set oFSO = Nothing