NO

Author Topic: How to Rewrite a batch file to C?  (Read 2260 times)

Editec

  • Guest
How to Rewrite a batch file to C?
« on: January 09, 2012, 12:20:19 pm »
I started from batch file we gotta start somewhere right? i got the hang of it kind of and i'd like to rewrite it now in C if its possible if so how?

if you need info on it : i made this batch to make a game called minecraft store its data in the same directory the .bat file was run from.
it also made sure certain folders were present & checked for java on systems etc am sure  you  can read the code as is so ill save you the time. thanks in advance for your time & help.


my windows batch file:
Code: [Select]
@echo off

echo %~n0%~x0 started from Directory: %~d0%~p0 > nul
%~d0
cd %~d0%~p0

IF NOT EXIST ".minecraft\options.txt" GOTO loadit
IF EXIST ".minecraft\options.txt" GOTO options

:loadit
SET APPDATA=%~dp0
GOTO javacheck

:options
cls
echo Move .minecraft To Desktop temporarily? (Reduces lag)
SET /P input=Yes or No (Y/N):
IF '%input%' == 'Y' (
GOTO move
)
IF '%input%' == 'y' (
GOTO move
)
IF '%input%' == 'N' (
GOTO loadit
)
IF '%input%' == 'n' (
GOTO loadit
) ELSE (
GOTO error
)

:move
cls
echo Now Moving files to Temp. Directory please wait...
set tempo=%userprofile%\Desktop
XCopy ".minecraft" "%userprofile%\Desktop\.minecraft" /S /E /I /Y /Q
set APPDATA=%tempo%
goto javacheck

:error
echo.
echo Your input was invalid
echo.
PAUSE
GOTO options

:javacheck
IF NOT EXIST "Java" GOTO check1
IF EXIST "Java" GOTO pass1

:check1
IF NOT EXIST "%ProgramFiles%\Java\jre6" GOTO check2
IF EXIST "%ProgramFiles%\Java\jre6" GOTO pass2

:check2
IF NOT EXIST "%ProgramFiles(x86)%\Java\jre6" GOTO check3
IF EXIST "%ProgramFiles(x86)%\Java\jre6" GOTO pass3

:check3
IF NOT EXIST "%ProgramFiles%\Java\jre7" GOTO fail
IF EXIST "%ProgramFiles%\Java\jre7" GOTO pass4

:fail
ECHO No Java has been found on the System or MCP Diretory.
pause

:pass4
"%ProgramFiles%\Java\jre7\bin\java.exe" -jar "Launcher\Minecraft.jar"
goto cleanup

:pass3
"%ProgramFiles(x86)%\Java\jre6\bin\java.exe" -jar "Launcher\Minecraft.jar"
goto cleanup

:pass2
"%ProgramFiles%\Java\jre6\bin\java.exe" -jar "Launcher\Minecraft.jar"
goto cleanup


:pass1
:input
cls
echo Examples 512MB=1/2GB 1024MB=1GB 2048MB=2GB
echo Don't use more RAM then what your computer has!
echo.
set INPUT=
set /P INPUT=Allocate How Much RAM To Minecraft?: %=%
if "%INPUT%"=="" goto input
Java\bin\java.exe -jar -Xms%INPUT%M -Xmx%INPUT%M "Launcher\Minecraft.jar"
goto cleanup

:cleanup
cls
IF EXIST "%localappdata%\temp\hsperfdata_%USERNAME%" (
RD /S /Q "%localappdata%\temp\hsperfdata_%USERNAME%"
) ELSE (
echo file not found moving on to next stage > nul
)

If not exist "%userprofile%\Desktop\.minecraft" exit
If exist "%userprofile%\Desktop\.minecraft" GOTO save
:save
XCopy "%userprofile%\Desktop\.minecraft\saves" ".minecraft\saves" /S /D /E /I /Y /Q
RD /S /Q "%userprofile%\Desktop\.minecraft"

CommonTater

  • Guest
Re: How to Rewrite a batch file to C?
« Reply #1 on: January 09, 2012, 12:38:53 pm »
Your best bet is to actually learn C... you'll find that batch files are pretty primitive beside what C can do for you.

Grab a good tutorial ( like THIS ) to get your feet wet, after you get a little taste of it, you may wish to continue on with a more indepth book (like THIS ) to go on to more indepth ideas and projects.
 
The big thing is to follow the lessons... read all of the text, type up and work with all the examples, do all of the exercises... when you get to the end of the tutorial or book, you should be able to write basic C programs.  From there you should challenge yourself with ever more complex and interesting projects... You'll find is a bit boring to learn but real rewarding when your first "solo" project comes to life...
 
 
« Last Edit: January 09, 2012, 12:42:37 pm by CommonTater »

Editec

  • Guest
Re: How to Rewrite a batch file to C?
« Reply #2 on: January 09, 2012, 12:42:21 pm »
Thanks for your help time to learn some C :)  hope i can handle it.
also say i learn it would my c program function the same if not better then my batch file?

CommonTater

  • Guest
Re: How to Rewrite a batch file to C?
« Reply #3 on: January 09, 2012, 12:43:33 pm »
Some things are better done with batch files.
Some things are better done with C.

As you learn you'll find out which is which...

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 760
Re: How to Rewrite a batch file to C?
« Reply #4 on: January 09, 2012, 07:23:50 pm »
Thanks for your help time to learn some C :)  hope i can handle it.
also say i learn it would my c program function the same if not better then my batch file?
Well, as CommonTater already mentioned, some things might work better as a batch file and some things might work better in a C (or any other compiled language) program...

In the simplest form, you could just make shell calls for most of the DOS commands. However that will present some problems getting error/result codes from those external commands back to your C program, as this is for the most part a one way street...

And you need to realize that replacing a lot of those DOS commands from the batch file, like XCOPY (which is an external program all by itself) or RD, "IF EXIST" (which are internal commands to the command shell) can be quite elaborate, which might need not only some knowledge of the C language itself but on some knowledge about how Windows itself operates...

What would be your intended goal of replacing that batch file with a C program?

Ralf