Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

NT40: Batch: Datum und Zeit in einer Zeile?

Empfohlene Antworten

Veröffentlicht

Hallo!

Folgendes Problem:

Ich möchte per Batch das aktuelle Datum und die aktuelle Uhrzeit in einer Zeile in einer Textdatei ausgeben:

Wenn ich eingebe: date /T >>C:\Log\Logdatei.log

time /T >>C:\Log\Logdatei.log

erscheinen Datum und Uhrzeit in der Logdatei untereinander (also Zeilenweise)

ist ja auch logisch.

Nur wie bekomme ich es hin, dass Datum und Uhrzeit hintereinander in der Logdatei stehen?

Und kann ich in der selben Zeile noch einen Text hinzufügen?

Z.B:Befehl: Heute ist Sonntag date /T time /T >>C:\Log\Logdatei.log

das in der Datei anschließend steht:

Heute ist Sonntag 08.01.2004 11:43

Moinsen...

ist einfach:

echo Heute ist der %DATE% und es sind %TIME% Uhr >> log.txt

Zumindest habe ich es so gestern noch gemacht... aber unter W2K. Aber denke mal das es so auch unter NT 4.0 klappt.

Meines Wissens gibt es in NT4 keine Umgebungsvariablen DATE und TIME.

Um sie zu erstellen kann man folgende zwei batches ausführen.

Ich hab sie vor einiger Zeit im Netz gefunden und leicht adaptiert.

**********************************************************

:: JUSTTIME.bat

@echo off

:: "JustTime.bat" puts the current time into the environment

:: Written under MSDOS 7 (Win95), should work on all recent versions.

:: The trick here is to use the first word of a command output

:: as the name of a batch file.

:: First find the line starting with "Current" that ends with the time

:: A single CR/LF is sent to TIME with ECHO. to end the TIME command

echo.|date|find /i "aktuelles" >ak##elles.bat

:: We now have "Current time is 1:45:59.82p" (without quotes) in CU##ENT.BAT

:: Since the first word is CURRENT, we need a command with that name ready

:: Now create a batch file named CURRENT.BAT

:: AKTUELLE.BAT will set the TIME variable to it's third argument (the time)

echo set date=%%3>aktuelles.bat

:: We now have "set time=%3" (without the quotes) in CURRENT.BAT

:: Now CALL our CU##ENT.BAT, which will in turn run CURRENT.BAT

call ak##elles.bat

:: Display the time we worked so hard to get

::echo %DATE%

:: Now delete both temporary batch files and erase the TIME variable

del ak??elles.bat > nul

::set time=

:: http://www.ericphelps.com

**********************************************************************************

:: JUSTDATE.BAT

@echo off

:: "JustTime.bat" puts the current time into the environment

:: Written under MSDOS 7 (Win95), should work on all recent versions.

:: The trick here is to use the first word of a command output

:: as the name of a batch file.

:: First find the line starting with "Current" that ends with the time

:: A single CR/LF is sent to TIME with ECHO. to end the TIME command

echo.|time|find /i "aktuelle" >ak##elle.bat

:: We now have "Current time is 1:45:59.82p" (without quotes) in CU##ENT.BAT

:: Since the first word is CURRENT, we need a command with that name ready

:: Now create a batch file named CURRENT.BAT

:: CURRENT.BAT will set the TIME variable to it's third argument (the time)

echo set time=%%2>aktuelle.bat

:: We now have "set time=%3" (without the quotes) in CURRENT.BAT

:: Now CALL our CU##ENT.BAT, which will in turn run CURRENT.BAT

call ak##elle.bat

:: Display the time we worked so hard to get

::echo %TIME%

:: Now delete both temporary batch files and erase the TIME variable

del ak??elle.bat > nul

::set time=

:: http://www.ericphelps.com

  • Autor

JUHU!

Dankeschön!

WindowsNT kennt die Variablen DATE und TIME nicht,

aber mit dem anderen Code konnte ich diese Variablen

erzeugen!

---Code-------

echo.|date|find /i "aktuelles" >ak##elles.bat

echo set date=%%3>aktuelles.bat

call ak##elles.bat

del ak??elles.bat > nul

d:\batch\tools\shutdown \\Server /r /t:1 /y /c

echo.|time|find /i "aktuelle" >ak##elle.bat

echo set time=%%2>aktuelle.bat

call ak##elle.bat

del ak??elle.bat > nul

REM Protokollieren

echo Server wurde neu gestartet am %DATE% um %TIME% Uhr >>d:\\logs\Logdatei.log

d:\batch\tools\sleep 5

set time=

---Code Ende----

klappt supi !

DANKE

  • Autor

Es ist auch möglich, die Variable DATE mit einer Zeile zu erzeugen:

for /f "Tokens=2" %a in ('date /t') do (set date=%a)

und hier der Einzeler für die Variable TIME (allerdings ohne Sekunden)

for /f %i in ('time /t') do (set time=%i)

  • Autor

Noch ne Frage:

Warum hab ich hier in der Variable TIME auch Sekunden,

 echo.|time|find /i "aktuelle" >ak##elle.bat
echo set time=%%2>aktuelle.bat
call ak##elle.bat
del ak??elle.bat > nul
[/PHP]

und hier

[PHP]for /f %i in ('time /t') do (set time=%i)

hab ich keine Sekunden ?

Noch als Hinweis:

Wenn die Befehle hier in der Kommandozeile eingegeben werden,

funktionieren sie. In Batch-Dateien muss das %-Zeichen doppelt geschrieben werden:

for /f %%i in ('time /t') do (set time=%%i)

  • Autor

Ok, Danke.

Ich hab mir dann folgendes Gedacht:

Der Befehl

echo.|time

gibt aus:

Aktuelle Zeit: 15:21:10,17

Geben Sie die neue Zeit ein:

c:\

(es muss also keine neue Zeit eingeben werden.)

Der Befehl

echo.|time /t

Gibt ebenfalls die Zeit aus, allerdings wieder ohne Sekunden.

Ok, nehme ich also den ersten Befehl: echo.|time

und den Befehl:

for /f "Tokens=3" %%a in ('"echo.|time"') do (set time=%%a)

ich erhalte dann als Ausgabe:

C:\>(set time=15:21:10,17 )

C:\>(set time=die )

die Variable time erhält also zuerst den Wert 15.21:10,17 (also die Uhrzeit, so wie ich es will) Die FOR Schleife haut dann aber in einem zweiten Durchgang das Wort "die" in die Variable.

Wie bekomme ich es hin, dann der FOR Befehl nach einem Lauf aufhört?

Original geschrieben von Thombo

Wie bekomme ich es hin, dann der FOR Befehl nach einem Lauf aufhört?

Ersetze echo.|time durch diese Zeile :

echo.|time|find "Aktuelle"

Dies wirst du aber aufgrund der Anführungszeichen nicht in eine Zeile kriegen, deswegen schreib das Ergebnis in eine Datei und lese dann in der for Schleife aus ihr.

also

echo.|time|find "Aktuelle >Datei

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.