我有几个文件夹,它们包含扩展名为.mdb和.mpg(以及更多)的文件。我需要一个输出文件夹名称(第1列),。mdb文件名(第2列)和.mpg文件名(第3列)的csv文件。 .mpg是计数中的倍数
所以输出结果如下:
output.csv
A B C
folder1 abc xyz
hgf
pqr
folder2 lala didi
loc
A,B和C是列。此外,如果可能的话,换行会很棒。使用Windows 7。
我在做
for /f "tokens=*" %%g in ('dir /b /a:d "*"') do (
forfiles /m *.mdb /c "cmd /c echo @fname>>output.csv"
)
答案 0 :(得分:0)
假设每个文件夹只有一个.mdb
,而您不想处理文件夹,其中不存在.mdb
:
echo off
setlocal enabledelayedexpansion
(
for /r %%d in (*.mdb) do (
set first=yes
set d=%%~pd
for %%z in ("!d:\=.!") do set d=%%~xz
<nul set /p =!d:.=!,%%~nd,
for %%f in ("%%~pd\*.mpg") do (
if defined first (
echo %%~nf
set "first="bre
) else (
echo ,,%%~nf
)
)
echo(
)
)>output.csv
相同的代码和解释(在每一行下):
@echo off
setlocal enabledelayedexpansion
(
REM enclose the whole code in parantheses ...<1>
for /r %%d in (*.mdb) do (
REM for every found .mdb file in folder or subfolders
REM /r = recursiv (including subfolders)
set first=yes
REM just a flag to identify the first line
set d=%%~pd
REM get path of the file
for %%z in ("!d:\=.!") do set d=%%~xz
REM ugly code for getting the last element of the path
REM by replacing \ with . so it looks like a filename
REM %%~xz gets the "extension" of that fake-"file"
<nul set /p =!d:.=!,%%~nd,
REM using set /p trick to write a line without linefeed
REM write the last path-element (with the dot removed) <comma> name of the mdb-file <comma>
for %%f in ("%%~pd\*.mpg") do (
REM for every .mpg in the found folder
if defined first (
REM if it is the first line (where the folder and the .mdb has to be written too)
echo %%~nf
REM append the name of the .mpg to the already written <folder>,<mdb-name>,
set "first="bre
REM delete the first-line-flag
) else (
echo ,,%%~nf
REM write a complete line
)
)
echo(
REM write the awesome linefeed
)
REM <1>... to write it's complete output to a file
)>output.csv
setlocal delayedexpansion
请参阅here。