Batch - Kurze Frage

BridaX

Captain
Registriert
Jan. 2009
Beiträge
3.844
Hallo,

ich habe mich vor wenigen Wochen das erste mal an eine Batch-Datei gewagt. Das "Programm" soll einfach nur als Zähler für die Arbeit dienen. Meine Lösung war mit der Formel "X+1" das Ergebnis in ein Textdokument zu schreiben und dieses dann auszulesen. Zuzüglich noch ein paar Spielereien...

Ursprünglich steht eine 0 im Textdokument, nach drücken der NUM-1 Taste wird aus der "0" im textdokument eine "1" ("X+1"). Beim nächsten drücken wird aus der "1" eine "2" usw...

Hier kurz die eigentliche Funktion:
:1a
cls
set /p count=<Count.txt
set /a final=%count%+1
echo %final% >count.txt
goto classic

Das einzige Problem auf das ich gestoßen bin, ist wenn der PC (unerwartet) heruntergefahren wird.

Das Textdokument wurde anscheinend zuvor nicht gespeichert und es erscheint beim nächsten Start der Batch (bzw. des Textdokuments) ein veralteter Zählerstand und der eigentliche ist weg. Ich bräuchte also einen Befehl, der nach dem Setzten eines Werts auch das Textdokument speichert - finde aber nichts dazu :(

Wen es interessiert hier das "Programm":

Ordner "FixCounter" auf dem Desktop erstellen.
Die Batch "FixCounter.bat" erstellen und in den Ordner legen.
Verknüpfung "FixCounter.lnk" im selben Ordner erstellen.
Batch ausführen.
- > Unter C:\ wird ein Verzeichnis "FixCounter" mit der .txt erstellt.


:head

@echo off
COLOR 0F

:Exist
IF EXIST C:\FixCounter\Saves goto classic
IF NOT EXIST C:\FixCounter\Saves goto start


:start

echo Begin initializing?
echo.
echo (9) No - Exit
echo (1) Yes - C:\FixCounter
echo.
choice /C 19 /N

if errorlevel 255 goto start
if errorlevel 2 call exit
if errorlevel 1 goto installation
if errorlevel 0 goto start

:installation
echo.
echo Initializing...
echo.
if not exist "C:\FixCounter" mkdir C:\FixCounter
if not exist "C:\FixCounter\Saves" mkdir C:\FixCounter\Saves
copy /-y %USERPROFILE%\Desktop\FixCounter\fixcounter.bat C:\FixCounter
copy /-y %USERPROFILE%\Desktop\FixCounter\fixcounter.lnk %USERPROFILE%\Desktop
cd C:\FixCounter
echo 0 >count.txt
echo.
cls
echo moved *.bat file
echo created new *.txt
echo New Path: C:\FixCounter
echo.
echo.
pause
echo.
goto installinfo


:1a
mode con: cols=30 lines=10
cls
set /p count=<Count.txt
set /a final=%count%+1
echo %final% >count.txt
goto classic

:Classic
cd C:\FixCounter
mode con: cols=30 lines=10
cls
echo (2) Advanced-Mode
echo (1) FixCount

echo.

set /p count=<Count.txt
echo Fix count: %count%

choice /C 12 /N

if errorlevel 255 goto classic
if errorlevel 2 goto choice
if errorlevel 1 goto 1a
if errorlevel 0 goto classic

:choice
cd C:\FixCounter
mode con: cols=30 lines=10
cls
echo (1) Count a first fix
echo (2) Save fix count
echo (3) Load save file
echo (4) Classic-Mode
echo (5) Exit
echo (8) Delete save file
echo (9) Clear counter
echo.

set /p count=<Count.txt
echo Fix count: %count%

choice /C 123456789 /N

if errorlevel 255 goto 0
if errorlevel 9 goto 9
if errorlevel 8 goto 8
if errorlevel 7 goto 7
if errorlevel 6 goto 6
if errorlevel 5 goto 5
if errorlevel 4 goto 4
if errorlevel 3 goto 3
if errorlevel 2 goto 2
if errorlevel 1 goto 1
if errorlevel 0 goto 0

:1
mode con: cols=30 lines=10
cls
set /p count=<Count.txt
set /a final=%count%+1
echo %final% >count.txt
goto choice

:2
mode con: cols=30 lines=10
cls
for /F "usebackq tokens=1,2,3 delims=-" %%I IN (`echo %date%`) do echo %%I - Saved!
echo.
echo %count% > C:\FixCounter\Saves\%date%.txt
echo.
echo.
pause
goto choice


pause

:3
mode con: cols=30 lines=10
cls
echo (1) Enter a date (DD.MM.YYYY)
echo (2) Show save files
echo (3) Back
echo.
choice /C 123 /N
echo.
if errorlevel 255 goto 0
if errorlevel 3 goto choice
if errorlevel 2 goto saves
if errorlevel 1 goto 3a
if errorlevel 0 goto 0
echo.

:3a
echo.
set /p show= Enter date:
echo.
cd C:\FixCounter\Saves
echo Counts:
type %show%.txt
set /p save=<%show%.txt
cd C:\FixCounter
echo %save% > count.txt
echo.
echo.
pause
goto choice

:4
goto classic

:5
call exit

:6
goto choice


:7
goto choice


:8
mode con: cols=30 lines=10
cls
echo.
set /p delete= Delete date (DD.MM.YYYY):
echo.
cd C:\FixCounter\Saves

IF NOT EXIST C:\FixCounter\Saves\%delete%.txt goto 8a
echo Are you sure to delete: %delete%.txt ?
echo.
echo (1) Delete
echo (2) Cancel

choice /C 12 /N

if errorlevel 255 goto choice
if errorlevel 2 goto choice
if errorlevel 1 del C:\FixCounter\Saves\%delete%.txt
if errorlevel 0 goto choice

pause

goto choice


:8a
mode con: cols=30 lines=10
echo no such file
echo.
pause
goto choice


:9
echo 0 >count.txt
goto choice

:0
Oups... There was an error!
Pause
goto choice

:saves
mode con: cols=60 lines=0
cls
echo Saves:
echo.
cd C:\FixCounter\Saves
dir /b
cd C:\FixCounter
echo.
echo.
echo (1) Back
echo (2) Cancel
echo.

choice /C 12 /N

if errorlevel 255 goto 0
if errorlevel 2 goto choice
if errorlevel 1 goto 3
if errorlevel 0 goto 0

:installinfo
cls
echo There should be a shortcut "FixCounter" on your desktop!
echo.
echo Programm will be started
echo.
pause
echo.
start c:\FixCounter\fixcounter.bat
 
Zuletzt bearbeitet:
Respekt, dass du dir das echt in Batch antust.

Mein klassischer Verweis auf die PowerShell, und dass man das da viel einfacher umsetzen kann, hilft dir wohl eher nicht?
Da hast du zB das cmdlet "Add-Content", oder "Out-File -Append".
 
wirelessy schrieb:
Respekt, dass du dir das echt in Batch antust.

Haha ja ^^

Ich hatte mich mal an einem ruhigen Samstag Zuhause hingesetzt. Dann hat mich während dessen ein Fieber gepackt und ich habe 10 Stunden "durchgebastelt". Hat echt Spaß gemacht - habe sonst nichts mit Programmieren am Hut.

In PowerShell müsste ich mich auch von 0 einlesen.
 
Zuletzt bearbeitet:
Zurück
Oben