1

I have a bat file that I want to convert into an exe so I can add a icon and a name. But I also need it to accept parameters like my bat file.

Here's the bat file receive parameters like this:%*

The bat file works fine on it own.

@echo off
title Parsing...
set v=%temp%\cmd.vbs
set d=%temp%\decode.wsc
(echo ^<?xml version="1.0"?^>^<component^>^<?component error="true" debug="true"?^>
echo ^<registration description="Url Decode Helper" progid="JSEngine.Url" version="1.0" classid="{80246bcc-45d4-4e92-95dc-4fd9a93d8529}" /^>
echo ^<public^>
echo ^<method name="decode"^>^<PARAMETER name="s"/^>^</method^>
echo ^</public^>
echo ^<script language="JScript"^>
echo ^<!^[CDATA^[
echo var description=new UrlEncodeDecodeHelper;
echo function UrlEncodeDecodeHelper^(^)^{this.decode=decode;^}
echo function decode^(s^)^{return decodeURIComponent^(s.replace^(/\+/g," "^)^);^}
echo ^]^]^>
echo ^</script^>
echo ^</component^>)>%d%
(echo Set JSEngine=GetObject^("Script:C:\Users\vanes\AppData\Local\Temp\decode.wsc"^)
echo Set obj=WScript.CreateObject^("WScript.Shell"^)
echo stre=JSEngine.decode^(Wscript.Arguments^(0^)^)
echo str=Mid^(stre,5^)
echo If Len^(stre^)^>5 Then
echo 'Remove the ^(^'^) from run and move it to para for it to run without confirmation
echo para^(^)
echo 'run^(^)
echo Else
echo obj.Run^("cmd.exe"^)
echo End If
echo.
echo Function para^(^)
echo MsgBox str,vbOKOnly+vbInformation+4096,"Code passed by program:"
echo user=MsgBox^("Do you allow it to pass to the Command prompt and run?",vbQuestion+4096+vbYesNo+vbDefaultButton2,str^)
echo If user=vbYes Then
echo run^(^)
echo Else
echo End If
echo End Function
echo Function run^(^)
echo obj.Run^("cmd.exe /k " ^& str^)
echo End Function)>%v%

rem creates the wsc and vbs files needed.

wscript %v% %*
del %v% %d%

I've tried all sorts of converters but they don't allow me to pass parameters. Is there any other converter or way to do it manually that will allow me to pass parameters into the exe file created?

I don't need it to have a icon and name right away I can add them through Resource hacker.

I'm trying to make a URL parser that will parser a URL like cmd:shutdown /s so the command shutdown /s will be sent into the command prompt and run. But only if the user allows it to be passed to the cmd! Here the idea_ https://youtu.be/46pBeyHKQuQ

User not found
  • 479
  • 3
  • 15
  • 4
    A batch file that generates a VBS and a WSC and then is EXE wrapped is quite the Rube Goldberg. Rewriting it in C# would be much tidier. It's not that hard to learn. Another option is to boil it all down to a single VBS file and then use the VBSEdit Exe converter: https://www.vbsedit.com/faq/convertexe.asp – LesFerch Sep 24 '21 at 11:49
  • But can I launch vbs directly from a browser? Well because it's a url parser. Launching a bat file can be done by just referencing it but vbs doesn't do the same. Maybe I can try learning c# then. – User not found Sep 26 '21 at 13:41
  • You can run VBS code directly within an HTA. If the goal is to provide a GUI front end to a script, then an HTA is a good choice. If you can provide more details about what you're doing (or trying to do) that would help to provide better options. – LesFerch Sep 26 '21 at 23:15
  • I don't need a GUI because I just need to ask the user whether they allow or deny the command to be passed into cmd. – User not found Sep 27 '21 at 03:54
  • This appears to cover the topic: https://stackoverflow.com/questions/7087728/custom-protocol-handler-in-chrome – LesFerch Sep 28 '21 at 01:56
  • Yeah it's related but I know how to set it up my problem is that I don't know how to create a parser that will parse commands like "cmd:echo hello world" I can't pass it directly into cmd.exe by editing the registry value to (cmd.exe "%1") cause that results in this "cmd:echo%20hello%20world" being sent into cmd.exe which causes an error. Reference - https://stackoverflow.com/a/37601807/15570769 – User not found Sep 28 '21 at 07:08
  • Start with the C# code for myprogram.exe that is shown in article 7087728 and just add a bit of string handling code to parse that parameter. It's a pretty easy project and will get you a result the same as shown in that YT video. – LesFerch Sep 28 '21 at 12:40

2 Answers2

0

I tried Bat To Exe Converter from MajorGeeks.com and it seems to work fine with parameters. One thing I noticed is that when passing arguments to a batch file, %* doesn't include the path of the batch file being run, but the converted exe does.

0

In this case, the best option may be to "convert" your batch file to an exe by rewriting it in C#. Below is a simple C# program you can use with your URL protocol handler. It converts the URI encoded parameter to plain text, splits off the command by looking for the protocol handler name (assumed to be "cmd:"), and then executes the command using Cmd /c. If you don't want a Cmd console screen popping up, then you could modify this code to run the command directly with a bit more parsing. And, of course, you can add a confirmation prompt as well.

using System;
using System.Diagnostics;

namespace Parser
{
    class Program
    {
        static void Main(string[] args)
        {
            string x = System.Uri.UnescapeDataString(Environment.CommandLine);
            string[] y = x.Split(new string[] {"cmd:"}, StringSplitOptions.None);
            Process.Start("Cmd.exe", " /c " + y[1]);
        }
    }
}

Here is the URL protocol handler reg entries I used to test this:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\SOFTWARE\Classes\cmd]
"URL Protocol"="\"\""
@="\"URL:cmd Protocol\""

[HKEY_CURRENT_USER\SOFTWARE\Classes\cmd\DefaultIcon]
@="\"parser.exe,1\""

[HKEY_CURRENT_USER\SOFTWARE\Classes\cmd\shell]

[HKEY_CURRENT_USER\SOFTWARE\Classes\cmd\shell\open]

[HKEY_CURRENT_USER\SOFTWARE\Classes\cmd\shell\open\command]
@="C:\\Parser\\Parser.exe %1"

BTW, you can run a .vbs via the Shell Open registry key by prefixing it with Wscript.exe or Cscript.exe. However, the browser will then ask if you want to run Windows Scripting Host. If you create your own exe, the browser will show the exe name (and you can add an icon) so an exe is probably the way you'll want to go.

LesFerch
  • 1,540
  • 2
  • 5
  • 21