我在启动时运行此脚本以获取运行它的计算机的日期、时间和 Windows 版本:
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
目标是避免在该计算机已注册的情况下再次注册数据,因此,每次启动时删除旧数据并用最新数据替换。
以下带注释的批处理文件可用于此任务:
@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
注1: 批处理文件可能同时被多台计算机运行,导致只有一个批处理文件可以读/写访问数据文件,而其他批处理文件则无法读/写数据文件。上面的批处理文件考虑了这一点,但并不是 100% 安全。最好用 C 或 C++ 编写一个 Windows 控制台应用程序,该应用程序处理共享文件访问拒绝,并等待一小段时间以使其他进程有时间完成,并使用带有计数器的循环来避免在无限循环中运行文件受到较长时间的读和/或写保护。
注 2: 计算机名称可以包含分号,如果数据文件应该是对此特殊也有效的 CSV 文件,则在将计算机名称写入数据文件时需要将计算机名称括在
"
中用例。上面的批处理文件不是为正确处理带有 CSV 规范分号的计算机名称而设计的,但它可以处理名称中带有“&”号的正确计算机名称。
阅读我的答案午夜后时间设置不正确,了解命令行的说明,以特定格式获取当前日期/时间,以确保所有日期/时间数据始终以国际日期写入数据文件/时间格式。
要了解所使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完整、仔细地阅读每个命令显示的帮助页面。
call /?
... 解释 %~n0
... 参数 0 的名称,即不带文件扩展名且不带路径的批处理文件名del /?
echo /?
endlocal /?
findstr /?
for /?
goto /?
if /?
move /?
rem /?
robocopy /?
set /?
setlocal /?
sort /?
ver /?
另请参阅: