0

I run this script on startup to get date, time and Windows version for the computer running it:

ver >c:\Temp\TempVersionFile.txt
FOR /f "tokens=4 delims=] " %%a IN (c:\Temp\TempVersionFile.txt) DO set VAR=%%a
echo %COMPUTERNAME%;%DATE%;%TIME%;%VAR% >> \\mypath\myfile.txt
del c:\Temp\TempVersionFile.txt
exit

The goal would be to avoid registering again the data if the said computer already is registered, and so, remove older data and replace it by the newest one at every startup.

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • 2
    Just to be clear, `ver` has never technically returned the version of Windows. It simply returns the version of Windows your host `cmd.exe` was supposed to be bundled with. Although less simple now, the executable file could be copied over to another/different version of Windows, and return an incorrect string for you. – Compo Jun 20 '22 at 09:45

1 Answers1

0

The following commented batch file could be used for this task:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "DataFile=\\server\share\MyDataFile.txt"
set "VersionInfo="

rem The command line below for getting the version information works even for Windows XP.
for /F "tokens=2 delims=[]" %%G in ('ver') do for /F "tokens=2" %%H in ("%%G") do set "VersionInfo=%%H"
rem Do nothing if the command line above fails which is very unlikely.
if not defined VersionInfo goto EndBatch

rem Do not change the data file on containing already a line with
rem the name of this computer and identical version data. Date
rem and time are not updated in this case which should not matter.
if exist "%DataFile%" %SystemRoot%\System32\findstr.exe /I /R /X /C:"%COMPUTERNAME%;.*;%VersionInfo%" "%DataFile%" >nul && goto EndBatch

rem Get current date/time in the format yyyy-MM-dd;HH:mm:ss independent
rem on which country/region is configured for the current user account.
for /F "tokens=1-6 delims=/: " %%G in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do set "DateTime=%%I-%%H-%%G;%%J:%%K:%%L" & goto CheckFile

:CheckFile
rem Create the data file if not existing at all with the data of current computer.
if not exist "%DataFile%" goto AppendData

rem Create a copy of the data file in temporary files directory of the
rem current user with omitting the line of the current computer. Then
rem move the filtered data file back to original storage location. If
rem that process fails because of the data file is opened currently by
rem another process which denies read and/or write access on the data file,
rem then exit processing of the batch file without updating the data file.
rem There will be one more chance in the near future to update the data file.
(
    %SystemRoot%\System32\findstr.exe /B /I /L /V /C:"%COMPUTERNAME%;" "%DataFile%" >"%TEMP%\%~n0.tmp" 2>nul
    if not exist "%TEMP%\%~n0.tmp" goto EndBatch
    if exist "%TEMP%\%~n0.tmp" move /Y "%TEMP%\%~n0.tmp" "%DataFile%" >nul 2>nul
    if exist "%TEMP%\%~n0.tmp" del "%TEMP%\%~n0.tmp" & goto EndBatch
) 2>nul

:AppendData
rem Append the data of current computer to the data file and next sort the
rem data file to have the lines sorted by computer names in the data file.
setlocal EnableDelayedExpansion
(echo !COMPUTERNAME!;!DateTime!;!VersionInfo!>>"%DataFile%") 2>nul
endlocal
%SystemRoot%\System32\sort.exe "%DataFile%" /O "%DataFile%" 2>nul

:EndBatch
endlocal

Note 1: The batch file could be run by multiple computers exactly at the same time which results in only one batch file can read/write access the data file while the others fail to read/write the data file. The batch file above takes that into account, but not 100% fail safe. It would be better to code a Windows console application in C or C++ which handles shared file access denials with waiting a short time to give the other process the time to finish and with using a loop with a counter to avoid running in an endless loop on file being protected for read and/or write for a longer time.

Note 2: The computer name could contain a semicolon which would require enclosing the computer name in " on writing the computer name into the data file if the data file should be a CSV file which should be valid also for this special use case. The batch file above is not designed for correct handling computer names with a semicolon regarding to CSV specification, but it handles correct computer names with an ampersand in name.

Read my answer on Time is set incorrectly after midnight for an explanation of the command line to get current date/time in a specific format to make sure that all date/time data are written into the data file always in the international date/time format.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • call /? ... explains %~n0 ... name of argument 0, i.e. batch file name without file extension and without path
  • del /?
  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • goto /?
  • if /?
  • move /?
  • rem /?
  • robocopy /?
  • set /?
  • setlocal /?
  • sort /?
  • ver /?

See also:

Mofi
  • 46,139
  • 17
  • 80
  • 143