如何使用命令提示符将解析的数据组织到表中?

How to organize parsed data into tables using command prompt?

我正在尝试在 excel 中创建一个 table,它将文件夹目录读取为字段 headers,并将内容列为行数据。我正在尝试解析包含员工培训证书的现有工作部分文件夹,并将它们的列表编译成 table.

REM cd /This is my workspace directory to organize into a table

dir /s *cert1 /b /o:gn >query.csv & dir /s *cert2 /b /o:gn >>query.csv

这创建了一个输出,但它仍然很乱并且难以排序。我想按文件夹描述 tables,但我找不到在命令提示符下执行此操作的简单方法。

此代码使用相对路径。您可以更改绝对值,在 MyPath 变量的开头添加“\”。

set "MyPath=Main_Directory\Section"

变化:

set "MyPath=\Main_Directory\Section"


make_table_csv.cmd:

@echo off

setlocal EnableDelayedExpansion

set "MyPath=Main_Directory\Section"

set "header="
For /f %%F in ('dir /b %MyPath%') do if not defined header (set header="%%~F") else (set header=!header!,"%%~F")

echo %header%

set /A C=0, MaxR=0
For /f %%F in ('dir /b %MyPath%') do (
  Set /A R=1,C+=1  
  For /f %%G in ('dir /b %MyPath%\%%F') do set M[!R!,!C!]=%%G & set /A R+=1
  if !R! GTR !MaxR! set /A MaxR=!R!-1
)

rem echo %MaxR% %C%

For /L %%R in (1,1,%MaxR%) do (
  set "Row="
  For /L %%C in (1,1,%C%) do if not defined Row (set Row="!M[%%R,%%C]!") else (set Row=!Row!,"!M[%%R,%%C]!")
  echo !Row!
)

复制此代码,例如,在“make_table_csv.cmd”

执行批处理文件并查看输出:

C:\Users\fposc\AppData\Local\Temp>make_table_csv.cmd
"Person1","Person10","Person11","Person12","Person13","Person14","Person15","Person2","Person3","Person4","Person5","Person6","Person7","Person8","Person9"
"person_Certificate1.pdf ","person_Certificate1.pdf ","person_Certificate1.pdf ","person_Certificate1.pdf ","person_Certificate1.pdf ","person_Certificate1.pdf ","person_Certificate1.pdf ","person_Certificate1.pdf ","person_Certificate1.pdf ","person_Certificate1.pdf ","","person_Certificate1.pdf ","","person_Certificate1.pdf ",""
"person_Certificate2.pdf ","person_Certificate2.pdf ","","person_Certificate2.pdf ","person_Certificate2.pdf ","","person_Certificate2.pdf ","person_Certificate2.pdf ","person_Certificate2.pdf ","person_Certificate2.pdf ","","person_Certificate2.pdf ","","",""
"person_Certificate3.pdf ","person_Certificate3.pdf ","","person_Certificate3.pdf ","person_Certificate3.pdf ","","person_Certificate3.pdf ","person_Certificate3.pdf ","person_Certificate3.pdf ","person_Certificate3.pdf ","","person_Certificate3.pdf ","","",""
"person_Certificate4.pdf ","","","person_Certificate4.pdf ","person_Certificate4.pdf ","","person_Certificate4.pdf ","","","","","person_Certificate4.pdf ","","",""
"person_Certificate5.pdf ","","","","person_Certificate5.pdf ","","","","","","","person_Certificate5.pdf ","","",""

C:\Users\fposc\AppData\Local\Temp>

您可以制作一个“csv”文件:

make_table_csv.cmd > table.csv

然后您可以在 excel 中导入“table.csv”或在 excel 中创建一个 link。

如需更改,请在评论中提出。