开始- <# :
- cls&echo off&cd /d "%~dp0"&rem 编码ANSI
- powershell -NoProfile -ExecutionPolicy bypass "[IO.File]::ReadAllText(\"%~f0\",[Text.Encoding]::GetEncoding('GB2312'))|Invoke-Expression"
- pause
- exit
- #>
- $csvfile=".\..\记录.csv";
- $txtfile=".\文档\文件.txt";
-
- $ip='null';
- $wmi=Get-WmiObject -Class Win32_NetworkAdapterConfiguration|?{($_.IPEnabled -eq $true) -and ($_.Description -notmatch 'Virtual')};
- if($wmi.IPAddress -ne $null){
- $ip=$wmi.IPAddress[0];
- }
-
- $filename='null';
- $author='null';
- $version='null';
- $enc=[Text.Encoding]::GetEncoding('GB2312');
- if(test-path -literal $txtfile){
- $text=[IO.File]::ReadAllText($txtfile, $enc);
- $m=[regex]::match($text, '@名称@=([^\r\n]+)');
- if($m.Success){$filename=$m.groups[1].value;}
- $m=[regex]::match($text, '@作者@=([^\r\n]+)');
- if($m.Success){$author=$m.groups[1].value;}
- $m=[regex]::match($text, '@版本@=([^\r\n]+)');
- if($m.Success){$version=$m.groups[1].value;}
- }else{write-host ('"'+$txtfile+'" 未找到');}
-
- $parentfolder=(get-item -literal ".").Name;
-
- $starttime=(get-date).ToString('yyyy-MM-dd HH:mm:ss');
-
- $arr=@(
- $($env:USERNAME);
- $ip,
- $parentfolder,
- $filename,
- $author,
- $version,
- $starttime,
- "",
- "制作中",
- "0"
- );
-
- $enc=[Text.Encoding]::GetEncoding('UTF-8');
- $title='"本机当前用户名","本机IP地址","编号","名称","版本","作者","开始时间","结束时间","当前状态","制作次数"';
- if(-not (test-path -literal $csvfile)){
- $line='"'+($arr -join '","')+'"';
- $newtext=$title+"`r`n"+$line;
- [IO.File]::WriteAllText($csvfile, $newtext, $enc);
- }else{
- $oldtext=[IO.File]::ReadAllLines($csvfile, $enc);
- $isexist=$false;
- for($i=0;$i -lt $oldtext.count;$i++){
- $brr=$oldtext[$i].trim('"') -split '","';
- if($brr[2] -eq $parentfolder){
- $isexist=$true;
- $arr[9]=([int]$brr[9]+1).ToString();
- $oldtext[$i]='"'+($arr -join '","')+'"';
- }
- }
- if($isexist){
- [IO.File]::WriteAllLines($csvfile, $oldtext, $enc);
- }else{
- $line='"'+($arr -join '","')+'"';
- $newtext=$oldtext[0]+"`r`n"+$line;
- if($oldtext.count -ge 2){
- $newtext=$oldtext[0]+"`r`n"+$line+"`r`n"+($oldtext[1..($oldtext.count-1)] -join "`r`n");
- }
- [IO.File]::WriteAllText($csvfile, $newtext, $enc);
- }
- }
复制代码
|