使用Batch to csv输出文件夹名称和文件夹中特定格式的文件
Output folder name and files of specific format in folder using Batch to csv
我有几个文件夹,它们包含扩展名为 .mdb 和 .mpg(以及更多)的文件。我需要一个输出文件夹名称(第一列)、.mdb 文件名(第二列)和 .mpg 文件名(第三列)的 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"
)
假设,每个文件夹只有一个 .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
参见 。
我有几个文件夹,它们包含扩展名为 .mdb 和 .mpg(以及更多)的文件。我需要一个输出文件夹名称(第一列)、.mdb 文件名(第二列)和 .mpg 文件名(第三列)的 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"
)
假设,每个文件夹只有一个 .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
参见