Simple HTA script to resize folder of photos

501 views Asked by At

I am using a command line version of resize.exe (a program to resize images)

I am trying to create a simple HTA application that will receive user input and pass it to a CMD. The problem here is that my batch script is failing when placing it in the VBS.

Am I escaping this wrong or is there something else that needs to be added?

<html>
<head>
<title>HTA Test</title>
<HTA:APPLICATION 
 ID="objTest" 
 APPLICATIONNAME="HTA Test"
 SCROLL="no"
 SINGLEINSTANCE="yes"
>
</head>
<SCRIPT LANGUAGE="VBScript">

Sub TestSub

    Dim height
    Dim width
    height = h.Value
    width = w.Value

    myCMD = "CMD for /r ""C:\hta\photos\"" %%X in (*) do (""C:\hta\resize.exe"" -i""C:\hta\photos\%%~nX%%~xX"" -o""C:\hta\resized\%%~nX%%~xX"" -s800x600) PAUSE"

    Set WshShell = CreateObject("WScript.Shell")
    WshShell.exec(myCMD)

End Sub
</SCRIPT>
<body>
Width <input type="text" name="w" size="10" value="320"><br>
Height <input type="text" name="h" size="10" value="240"><br>
<input type="submit" name="" value="sub" onClick="TestSub">
</body>
2

There are 2 answers

0
Hackoo On

Here is the HTA that can write the batch file and execute it :

<html>
<head>
<title>HTA Photos Resizer by Hackoo</title>
<HTA:APPLICATION
SCROLL="no"
ICON="nslookup.exe"
SHOWINTASKBAR="yes"
SINGLEINSTANCE="yes"
WINDOWSTATE="no"
CAPTION="yes"
MAXIMIZEBUTTON="no"
MINIMIZEBUTTON="yes"
SYSMENU="yes"
BORDER="thin"
BORDERSTYLE="Normal"
CONTEXTMENU="no"
SELECTION="no">
<style type="text/css">
body {
font-family:Verdana;
font-size: 12px;
color: #49403B;
background: Cyan;
text-align: center;
}
</style>
</head>
<META HTTP-EQUIV="MSThemeCompatible" CONTENT="YES">
<SCRIPT LANGUAGE="VBScript">
Option Explicit
Dim Title,fso
Title = "HTA to Batch Resizer Photos by Hackoo"
Set fso = CreateObject("Scripting.FileSystemObject")
Sub Window_OnLoad()
    CenterWindow 350,260
    If Not fso.FileExists("resize.exe") Then
        MsgBox "You must check "& DblQuote("resize.exe") &" is in the same folder of this HTA",VbCritical+VbSystemModal,Title
        Exit Sub
    End If
End Sub
'************************************************************************************************
Sub CenterWindow(x,y)
    Dim iLeft,itop
    window.resizeTo x,y
    iLeft = window.screen.availWidth/2 - x/2
    itop = window.screen.availHeight/2 - y/2
    window.moveTo ileft,itop
End Sub
'************************************************************************************************
Sub ResizePhotos()
    Dim Title,fso,InputFolder,ParentFolder,OutputFolder,MyTab,height,width,objOutputFile,BatchFile,WshShell,Exec
    Title = "HTA to Batch Resizer Photos by Hackoo"
    BatchFile = "Resizer.bat"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set objOutputFile = fso.OpenTextFile(BatchFile,2,True)
    height = h.Value
    width = w.Value
    InputFolder = TxtInputFolder.value
    If InputFolder = "" Then
        MsgBox "You must select the source folder",VbCritical+VbSystemModal,Title
        Exit Sub
    End If
    ParentFolder = fso.GetParentFolderName(InputFolder)
    MyTab = Split(InputFolder,"\")
    OutPutFolder = ParentFolder & "\" & MyTab(UBound(MyTab)) & "_Resized"
    objOutputFile.WriteLine "@echo off & mode con cols=70 lines=6 & color 9B"
    objOutputFile.WriteLine "Title Batch Photos ReSizer"
    objOutputFile.WriteLine "set InputFolder="& InputFolder &""
    objOutputFile.WriteLine "set OutputFolder="& OutPutFolder &""
    objOutputFile.WriteLine "echo."
    objOutputFile.WriteLine "echo You have choosen to resize your photos in this resolution "& width &"x"& height &""
    objOutputFile.WriteLine "echo."
    objOutputFile.WriteLine "pause"
    objOutputFile.WriteLine "Cls & echo. & echo Resizing photos is in Progress ..."
    objOutputFile.WriteLine "if not exist %OutputFolder% MD %OutputFolder%"
    objOutputFile.WriteLine "for /r "& DblQuote(InputFolder) &" %%A in (*) do (resize.exe -i"& DblQuote(InputFolder &"\%%~nxA") &" -o"& DblQuote(OutputFolder &"\%%~nxA")&" -s"& width&"x"& height &")"
    objOutputFile.WriteLine "Start Explorer ""%OutputFolder%"""
    objOutputFile.Close
    Set objOutputFile = Nothing
    Set WshShell = CreateObject("WScript.Shell")
    Exec = WshShell.Run(BatchFile,1,True)
    fso.DeleteFile BatchFile
End Sub
'***********************************************************************
Function PickFolder(strStartDir)
    Dim SA,F
    Set SA = CreateObject("Shell.Application")
    Set F = SA.BrowseForFolder(0,"Choose the source folder",1,strStartDir)
    If (Not F Is Nothing) Then
        PickFolder = F.Items.Item.path
    End If
    Set F = Nothing
    Set SA = Nothing
End Function
'***********************************************************************
Sub BrowseSource_OnClick()
    Dim strStartDir
    strStartDir = "c:\Programs"
    TxtInputFolder.value = PickFolder(strStartDir)
End Sub
'***********************************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'***********************************************************************
</SCRIPT>
<body>
Select the source folder  :
<input type = "text" Value="c:\hta\photos" name = "TxtInputFolder" size="40"/><br><br>
<input type = "button" value = "Browse for the source Folder" Name="BrowseSource"><br><br>
Width <input type="text" name="w" size="6" value="320"><br>
Height <input type="text" name="h" size="6" value="240"><br><br>
<input type="submit" name="" value="Resize my photos" onClick="ResizePhotos">
</body>
1
Hackoo On

I just downloaded the Resize.exe and i made a little batch to test it on my PC and it works 5/5 with this batch, i know you tagged for HTA, so just test this batch and i will try to integrate it on the HTA.

Just give a try for this batch for this moment :

@echo off
Title Batch Photos ReSizer
set RootFolder=C:\hta
set InputFolder=C:\hta\Photos\
set OutputFolder=%RootFolder%\Resized-photos
set /p Width=Choose the Width :  
set /p Height=Choose the Height : 
echo.
echo You have choosen to resize your photos in this resolution %Width%x%Height%
echo.
pause
echo. & echo Resizing photos is in Progress ...
if not exist %OutputFolder% MD %OutputFolder%
for /r %InputFolder% %%A in (*) do ("C:\hta\resize.exe" -i"%InputFolder%\%%~nxA" -o"%OutputFolder%\%%~nxA" -s%Width%x%Height%)
pause
Start Explorer "%OutputFolder%"