2

前言:
事情在工作中被锁定了,我不能在 Excel 中使用 powershell 或任何宏/脚本。我不能将文件带到异地或在个人计算机上使用。我无权访问文件的原始程序。我似乎仅限于批处理脚本。

情况:
我在离线机器上有几百个带有系统信息的 csv 文件,我试图提取第 2 行,然后附加第 3、6 和 20 行的第 4 和第 5 字段。将其导出到新的 . .csv 文件

第一行在后续行中重复,因此我创建了一个函数来识别第 1 行的长度并将其存储在一个变量中以用作后续字符串的偏移量(第 3、6、20 行)。

我将各种行放入变量中,然后将变量回显到文件中。到目前为止,我只关心第 2 行和第 3 行,直到我弄清楚这一点。我要崩溃的地方是我似乎无法将变量用作另一个变量中的偏移量。这总是作为整个字符串出现。
例如:!Ln3:~%L1Len%!

甚至可以按照我尝试的方式做到这一点吗?
是否有另一种方法(在 Windows 批处理中)?

我认为必须有一种方法来使用findstr /C:"Edition ID" dc- .csv* (对于第 6 行)并将该行的其余部分输出到一个变量,但我还没有弄清楚。它也不适用于其他行上的数据,因为这些前导字符的文件中有多个实例。

如果没有,我将只回显行变量,然后手动删除 Excel 中的列。在这一点上可能更快,但我无法弄清楚这一点让我感到沮丧!

这是我当前的 .bat 文件:

@ECHO OFF
SETLOCAL enabledelayedexpansion

:: Set what the window title is so it isn't just cmd.exe
TITLE OS Info Extractor

:: Time variable to change the space in single-digit (AM) hours to zero
SET timestring=%time: =0%

::  Set the destination file name as OS_INFO_YYYYMMDD.csv
SET destfile="OS_INFO_%date:~10,4%%date:~4,2%%date:~7,2%_%timestring:~0,2%_%time:~3,2%.csv"

::  Set the source directory as current directory
SET sourcedir=%CD%

:: Create a count variable for file counting
SET count=1

:: TEST Create a varialbe for the OS name Test creating a variable with the offset string?
SET OSname=

:: Create Variables for the lines of text & length of line 1 for offset
SET Ln1=
SET Ln2=
SET Ln3=
SET Ln6=
SET Ln20=
SET L1Len=


ECHO.
ECHO This batch file will extract the OS information from dc-*.csv files located in:
ECHO  %sourcedir% 
ECHO and output to a new file called: %destfile%
ECHO.
ECHO Please ensure all source files are in the same directory
ECHO as this batch file, then press any key to begin...
pause >nul

:: Create csv column headers
ECHO "Source File","Computer Name"," ","Operating System"," ","Operating System Name 1","Operating System Name 2"," ","OS Edition ID"," ","OS Kernel Ver" >> %destfile%

:: For each file in the directory, do...
FOR %%a IN ("!sourcedir!\dc-*.csv") DO (
   SET contb=y
   SET contc=y
   SET Sourcefile=%%~nxa
   SET /p "Ln1="<"%%~a"
   ECHO.
   ECHO Processing file !count!:  %%~nxa 

   FOR /f "usebackqskip=1delims=" %%b IN ("%%a") DO IF DEFINED contb (
     SET Ln2=%%b
     SET contb=
     )

   FOR /f "usebackqskip=2delims=" %%c IN ("%%a") DO IF DEFINED contc (
     SET Ln3=%%c
     SET contc=
     )

:: Increment the file count
   SET /a count+=1

:: Get length of line 1
   CALL :Stringlength L1Len Ln1

:: check output of variables
   ECHO [Debug] character length of 1st line is !L1Len! 
   ECHO [Debug] output of 1st line is: !Ln1!
   ECHO [Debug] output of 2nd line is: !Ln2!
   ECHO [Debug] output of 3rd line is: !Ln3!
   ECHO [Debug] ^^!Ln3:~%%L1Len%%^^! output: !Ln3:~%L1Len%!
   ECHO.

:: Test creating a variable with the offset string? 
   SET "OSname=!Ln3:~%L1Len%!"
   ECHO [Debug] Test create new varaible ^^!OSname^^! from ^^!Ln3:~%%L1Len%%^^!
   ECHO [Debug] New variable output:   !OSname!

:: Output desired data to new line in file
   ECHO "!sourcefile!",!Ln2!,!Ln3:~%L1Len%! >> !destfile!
)

ECHO.
ECHO Task complete

ENDLOCAL
pause
exit

:: Function to count string length in a variable

:Stringlength <resultVar> <stringVar>
(   
    set "s=!%~2!#"
    set "len=0"
    for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
        if "!s:~%%P,1!" NEQ "" ( 
            set /a "len+=%%P"
            set "s=!s:~%%P!"
        )
    )
)
( 
    set "%~1=%len%"
    exit /b
)

源文件内容如下所示:

"ComputerName1","Operating System","Windows 7"
"ComputerName1","Operating System","Windows 7","Name","Windows 7 x64 Service Pack 1"
"ComputerName1","Operating System","Windows 7","Product Name","Windows Embedded Standard"
"ComputerName1","Operating System","Windows 7","Features","64 Bit Edition, Embedded, Terminal Services in Remote Admin Mode, Multiprocessor Free"
"ComputerName1","Operating System","Windows 7","Edition Type","Embedded"
"ComputerName1","Operating System","Windows 7","Edition ID","X15-82254"

最终目标:

“Source File”,”Computer Name”,” “,”Operating System”,” “,”Operating System Name 1”,”Operating System Name 2”,”OS Edition ID”,”OS Kernel Ver" 
“sourcefile1.csv”,”ComputerName1”,”Operating System”,”Windows 7”,”Name”,”Windows 7 Professional x64 Service Pack 1”,"Kernel Version",”Windows 7 Professional”,”X15-39034”,”6.1.7601.23403”
“sourcefile2.csv”,”ComputerName2”,”Operating System”,”Windows 7”,”Name”,”Windows 7 Professional x64 Service Pack 1”,"Kernel Version",”Windows 7 Professional”,”X15-37362”,”6.1.7601.17651”
“sourcefile3.csv”,”ComputerName3”,”Operating System”,”Windows 7”,”Name”,”Windows 7 x64 Service Pack 1”,"Kernel Version",”Windows Embedded Standard”,”X15-82254”,"Kernel Version",”6.1.7601.17965”
4

0 回答 0