Batch won't work properly after conversion to exe

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
eax22
Posts: 13
Joined: 15 Apr 2017 15:02

Batch won't work properly after conversion to exe

#1 Post by eax22 » 20 Apr 2017 04:11

Three batch files that work with each other won't do the job properly after being converted to exe files, but they do everything correctly in their bat form. I tried setting up both 64 bit and 32 bit conversions, but both won't work. How would I go about determining where the error is?

Yes, where they mention each other, I changed every instance of .bat to .exe in the code, so that's not it.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Batch won't work properly after conversion to exe

#2 Post by Compo » 20 Apr 2017 05:01

If you really do want an answer you'd probably have to provide the content of all of your batch files prior to 'conversion' and possibly even the name/version of the application you used together with the parameters/options you invoked for the 'conversion' process.

You may not be aware that these 'converters' don't really convert anything, they wrap the script into essentially a self extractor and run the script from a usually hidden location prior to usually deleting it. The most common problem therefore is scripts which do not specifically define paths or do not make them relative to their extracted locations as those are often unknown at the time or script writing.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Batch won't work properly after conversion to exe

#3 Post by ShadowThief » 20 Apr 2017 05:48

Obligatory "yeah... don't do that" post

eax22
Posts: 13
Joined: 15 Apr 2017 15:02

Re: Batch won't work properly after conversion to exe

#4 Post by eax22 » 20 Apr 2017 08:03

Batch 1:

Code: Select all

start /min x1\x2\image_c-1.bat
start /min x1\temp_storage\check.bat


Batch 2:

Code: Select all

set mdir=%~dp0
cd %mdir%
cd..
cd temp_storage
:getPID  [RtnVar]
setlocal disableDelayedExpansion
:getLock
set "lock=%temp%\%~nx0.%time::=.%.lock"
set "uid=%lock:\=:b%"
set "uid=%uid:,=:c%"
set "uid=%uid:'=:q%"
set "uid=%uid:_=:u%"
setlocal enableDelayedExpansion
set "uid=!uid:%%=:p!"
endlocal & set "uid=%uid%"
2>nul ( 9>"%lock%" (
  for /f "skip=1" %%A in (
    'wmic process where "name='cmd.exe' and CommandLine like '%%<%uid%>%%'" get ParentProcessID'
  ) do for %%B in (%%A) do set "PID=%%B"
  (call )
))||goto :getLock
echo %PID% >>pid.txt
ping localhost -n 1
set /p pid=<pid2.txt

for /r "%userprofile%\folder_22" %%a in (*.gif) do @copy /y "%%a"

taskkill /pid %pid%
erase pid.txt
erase pid2.txt


Batch 3:

Code: Select all

set mdir=%~dp0
cd %mdir%
:getPID  [RtnVar]
setlocal disableDelayedExpansion
:getLock
set "lock=%temp%\%~nx0.%time::=.%.lock"
set "uid=%lock:\=:b%"
set "uid=%uid:,=:c%"
set "uid=%uid:'=:q%"
set "uid=%uid:_=:u%"
setlocal enableDelayedExpansion
set "uid=!uid:%%=:p!"
endlocal & set "uid=%uid%"
2>nul ( 9>"%lock%" (
  for /f "skip=1" %%A in (
    'wmic process where "name='cmd.exe' and CommandLine like '%%<%uid%>%%'" get ParentProcessID'
  ) do for %%B in (%%A) do set "PID=%%B"
  (call )
))||goto :getLock
echo %PID% >>pid2.txt
ping localhost -n 1 >nul
set /p pid=<pid.txt
:loop
setlocal EnableDelayedExpansion
set /a value=0
set /a sum=0
FOR /R %1 %%I IN (*) DO (
set /a value=%%~zI/45000
set /a sum=!sum!+!value!
)
if %sum% gtr 2500 goto end_pic
goto loop
:end_pic
taskkill /pid %pid%
erase pid.txt
erase pid2.txt


I used Bat to Exe Converter v2.4.8 from http://www.f2ko.de

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch won't work properly after conversion to exe

#5 Post by aGerman » 20 Apr 2017 10:16

eax22 wrote:

Code: Select all

start /min x1\x2\image_c-1.bat
start /min x1\temp_storage\check.bat
Oh, why do I see extensions .bat? Didn't you say you converted everyting to .exe files? And what is x1 in reality, a relative or an absolute path?

eax22 wrote:

Code: Select all

set mdir=%~dp0
This expands to the path where the .bat file will be extracted after you run the .exe file. Probably somewhere in the %temp% folder.

You want to know my experinces with nonsense like bat2exe? Enjoy!: viewtopic.php?t=5908#p36858
Batch is an interpreted scripting language - period. If you want to have an .exe file you should realy learn a compilable language.

Steffen

eax22
Posts: 13
Joined: 15 Apr 2017 15:02

Re: Batch won't work properly after conversion to exe

#6 Post by eax22 » 20 Apr 2017 13:58

aGerman, I changed them to .exes as I said. The version you are seeing is the batch version, which I use to confirm that everything should work, there is literally no difference in those two, except the .bat/.exe difference.

I converted batch codes to exes successfully in the past, it's only rarely that I encounter problems. When I do, I hope that I can find solutions to them instead of starting up on thenewboston's tutorials on C++ ... although I'm planning to do that in the future.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Batch won't work properly after conversion to exe

#7 Post by Compo » 20 Apr 2017 16:14

Am I correct in assuming that Batch 2 refers to "image_c-1.bat" and Batch 3 refers to "check.bat"?

I agree with aGerman in respect to the content of Batch 1, are you sure that the relative path of the self extracting file "image_c-1.exe" is going to be "%~dp0x1\x2\" also how do you know that the relative path of the self extracting file "check.exe" is going to be "%~dp0x1\temp_storage\". I'm not saying that you don't just that your provided information does not tell us that. If you look at your code for Batch 2 you can clearly see that you are expecting to change to the directory named temp_storage which is relative to the batch file not to the exe file, this means that you must be expecting each exe to extract each bat file adjacent to itself into a known directory structure. This seems a little optimistic from my experience with these batch file wrappers.

eax22
Posts: 13
Joined: 15 Apr 2017 15:02

Re: Batch won't work properly after conversion to exe

#8 Post by eax22 » 20 Apr 2017 17:26

I tested this setup numerous times and it works perfectly fine, while in the batch form. So I think that it's safe to assume the paths are all set correctly? I may not be as techy as some of you guys on here so... plz extra patience.

What I noticed, is that Batch 2 and 3, when they are launched by Batch 1, seem like they don't know their true position unless I tell them. That is why I have

Code: Select all

set mdir=%~dp0 
cd %mdir%

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Batch won't work properly after conversion to exe

#9 Post by Compo » 21 Apr 2017 04:30

If you do not read and understand my last post then I'm afraid you will not get an answer.

The following works:

Code: Select all

C:\USERS\EAX22\DESKTOP\TEST
|   batch1.bat
|   
\---x1
    +---temp_storage
    |       check.bat
    |       
    \---x2
            image_c-1.bat

You now reportedly have the following structure, which doesn't work:

Code: Select all

C:\USERS\EAX22\DESKTOP\TEST
|   batch1.exe
|   
\---x1
    +---temp_storage
    |       check.exe
    |       
    \---x2
            image_c-1.exe

When you invoke batch1.exe it extracts batch1.bat to a secret location before running it, (it may even randomise it's filename too):

Code: Select all

C:\USERS\EAX22\APPDATA\LOCAL\TEMP\RANDOMNAME
    batch1.bat

At this time you want to invoke "C:\Users\eax22\Desktop\Test\x1\x2\image_c-1.exe" so it needs to reference that location not "C:\Users\eax22\AppData\Local\Temp\RandomName\x1\x2\image_c-1.exe".

Code: Select all

start "" /min "C:\Users\eax22\Desktop\Test\x1\x2\image_c-1.exe"
When you do that, it extracts image_c-1.bat to a secret location before running it, (it may even randomise it's file name too):

Code: Select all

C:\USERS\EAX22\APPDATA\LOCAL\TEMP\ANOTHERRANDOMNAME
    image_c-1.bat

When that batch file is run, you are setting the current directory to that of the running script, "C:\Users\eax22\AppData\Local\Temp\AnotherRandomName". Then you are stepping up a level to "C:\Users\eax22\AppData\Local\Temp" and then expecting to drop into "C:\Users\eax22\AppData\Local\Temp\temp_storage" which is most unlikely to exist.
At the same time this is happening you are wanting to invoke "C:\Users\eax22\Desktop\Test\x1\temp_storage\check.exe" so it needs to reference that location not "C:\Users\eax22\AppData\Local\Temp\RandomName\x1\temp_storage\check.exe".

Code: Select all

start "" /min "C:\Users\eax22\Desktop\Test\x1\temp_storage\check.exe"
When you do that, it extracts check.bat to a secret location before running it, (it may even randomise it's file name too):

Code: Select all

C:\USERS\EAX22\APPDATA\LOCAL\TEMP\DIFFERENTRANDOMNAME
    check.bat
When that batch file is run it's current directory is being set to "C:\Users\eax22\AppData\Local\Temp\DifferentRandomName".…etc.

The first thing you need to do to help simplify the process is to not 'convert' batch1.bat to an exe, this will then reference the correct paths to the executables:

Code: Select all

start "" /min "x1\x2\image_c-1.exe"
start "" /min "x1\temp_storage\check.exe"
You then need to restructure your other two individual batch files to ensure that they are not referencing each other or each others relative locations, which will be unknowns.

Code: Select all

…do that yourself
Then 'convert' to exe's again and try once more.

Post Reply