Batch Conversion issue

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Emo
Posts: 4
Joined: 22 Mar 2021 16:43

Batch Conversion issue

#1 Post by Emo » 23 Mar 2021 09:21

Hi all,
I have a Batch file to ping for all hostnames inside my domain by brings these hostnames from an external text file (hosts.txt) and it works properly while using it as a bat file, but after converting this file to an executable file (exe) and try to use it, its display an error message contain that (The system cannot find the file "hosts.txt" )
why does this happen? And how can fix this issue?
Here is the mentioned code:

Code: Select all

@echo off
title Ping Batch using "External Text File"
setlocal enableextensions enabledelayedexpansion
color 0a
Rem mode 125,35
mode con:cols=105 lines=30
TIMEOUT /T 1 /NOBREAK >nul
ECHO. ====================================================================================================== && ping -n 1 localhost >nul
ECHO. ^|^| ------====^<({ Welcome to Amin Muhammad Batches })^>====------ ^| ---===^<({ Created By :- })^>===--- ^|^| && ping -n 1 localhost >nul
ECHO. ^|====================================================================================================^| && ping -n 1 localhost >nul
ECHO. ^| - This tool will help you wherever you are,                   ^|                                    ^| && ping -n 1 localhost >nul
ECHO. ^|   just you must be an administrator on your domain.           ^|                                    ^| && ping -n 1 localhost >nul
ECHO. ^|                                                               ^|                                    ^| && ping -n 1 localhost >nul
ECHO. ^| - Ping Package Menu:-                                         ^|                                    ^| && ping -n 1 localhost >nul
ECHO. ^|                                                               ^|                                    ^| && ping -n 1 localhost >nul
ECHO. ^| - Now You can Ping Bulk Machines, just insert the text file   ^|                                    ^| && ping -n 1 localhost >nul
ECHO. ^|   (hosts.txt)that contains all hosts you need to ping it and  ^| Compatible with Windows 7 and later^| && ping -n 1 localhost >nul
ECHO. ^|   be sure that the file located in the same directory.        ^|                                    ^| && ping -n 1 localhost >nul
ECHO. ^|                                                               ^|                                    ^| && ping -n 1 localhost >nul
ECHO. ^|                                                               ^|                                    ^| && ping -n 1 localhost >nul
ECHO. ^|                                                               ^|                                    ^| && ping -n 1 localhost >nul
ECHO. ^|                                                               ^|                                    ^| && ping -n 1 localhost >nul
ECHO. ^|                                                               ^|                                    ^| && ping -n 1 localhost >nul
ECHO. ^|                                                               ^|                                    ^| && ping -n 1 localhost >nul
ECHO. ^|                                                               ^|                                    ^| && ping -n 1 localhost >nul
ECHO. ^|  (Write related choice to do what you want)                   ^|                                    ^| && ping -n 1 localhost >nul
ECHO. ^|                                                               ^|                                    ^| && ping -n 1 localhost >nul
ECHO. ^|                                                               ^|                                    ^| && ping -n 1 localhost >nul
ECHO. ====================================================================================================== && ping -n 1 localhost >nul
ECHO.
TIMEOUT /T 1 /NOBREAK >nul
echo.  You are about to Ping the whole machines in your file , Are you sure?
TIMEOUT /T 1 /NOBREAK >nul
echo.
echo.  You have 30 Seconds to choose Or the program will terminate
choice /c YN /t 30 /d n /m  "    Press 'Y' to Continue or press 'N' to Cancel "

If %ErrorLevel%==1 GoTo y
If %ErrorLevel%==2 GoTo n

:y
cls
TIMEOUT /T 1 /NOBREAK >nul
echo This Operation has been Confirmed.
TIMEOUT /T 1 /NOBREAK >nul
echo Now it will start after the below timeout.
TIMEOUT /T 5 /NOBREAK
cls

set /a "countOn=0"
set /a "countOff=0"


for /f %%i in (hosts.txt) do (
   set SERVER_ADDRESS_I=UNRESOLVED_IP
   set SERVER_ADDRESS_L=UNRESOLVED_IP
   SET bHOSTUP=0
   ping -n 1 %%i -w 2000 |find "TTL=" > NUL && SET bHOSTUP=1
   IF !bHOSTUP! equ 1 (
      set g=%%i
      CALL :HOSTUP %%i
   ) else (
      set g=%%i
      CALL :HOSTDOWN %%i 
   )
)

pause
GOTO EOF

:n
cls
color fc
TIMEOUT /T 1 /NOBREAK >nul
echo.  This Operation has been canceled
TIMEOUT /T 3 /NOBREAK
GOTO EOF


:HOSTUP
Rem add 1 number to countOn variable for each online machine
set /a "countOn+=1"

for /f "tokens=1,2,3" %%x in ('ping -a -n 1 %g% ^&^& echo SERVER_IS_UP') do (
    if %%x==Pinging set SERVER_ADDRESS_L=%%y
    if %%x==Pinging set SERVER_ADDRESS_I=%%z
    )

Echo %g% : %SERVER_ADDRESS_I% : Is Pingable 
echo.[%g% : %SERVER_ADDRESS_I% : is Online and Pingable]>>"Online Workstations Result for Ping.txt"
GOTO EOF


:HOSTDOWN
Rem add 1 number to countOff variable for each offline machine
set /a "countOff+=1"

for /f "tokens=1,2,3" %%x in ('ping -a -n 1 %g% ^&^& echo SERVER_IS_UP') do (
    if %%x==Pinging set SERVER_ADDRESS_L=%%y
    if %%x==Pinging set SERVER_ADDRESS_I=%%z
    )

Echo %g% : %SERVER_ADDRESS_I% : Is Offline or Not Exist
echo.[%g% : %SERVER_ADDRESS_I% : Is Offline or Not Exist]>>"Offline Workstations Result for Ping.txt"
GOTO EOF

:EOF
exit /B
and You must have another text file named "hosts.txt" contain at all hostnames separated by Enter button and this file located in the same directory

Question also asked on Computer Hope Forums.
Last edited by Emo on 23 Mar 2021 15:57, edited 1 time in total.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch Conversion issue

#2 Post by penpen » 23 Mar 2021 11:43

Essentially all bat2exe-converter i saw, only pack the file into a self extracting zip.
If you call the exe, then the batch file you wrote typically is extracted into a temporary folder (such as %TEMP% or %TMP%) and is automatically started.
Some of those tools also will ignore any command line parameter.
Depending on the code that starts the batch file and location the exe file is called from, you might be able to use "%CD%" "%~0" and extract the path.
If not, then you might extract the data using WMIC - but that also might be tricky.


penpen

Emo
Posts: 4
Joined: 22 Mar 2021 16:43

Re: Batch Conversion issue

#3 Post by Emo » 30 May 2021 22:29

Thanks for help

Post Reply