如何使用批处理获取文件父级父文件夹

时间:2019-09-17 10:44:24

标签: for-loop batch-file

我使用下面的批处理从3个Station文件夹中的多个.LOG文件生成CSV报告。该代码可以完美地生成报告,但是我也希望在报告中包含父文件夹(站号文件夹而不是TAG文件夹)。

批处理文件位于:

C:\Users\amein\Google Drive\Report BACKUP

日志文件路径:

C:\Users\amein\Google Drive\Report BACKUP\Station1\TAG\10092019.LOG
C:\Users\amein\Google Drive\Report BACKUP\Station2\TAG\10092019.LOG
C:\Users\amein\Google Drive\Report BACKUP\Station3\TAG\10092019.LOG
@echo off
cls
setlocal EnableDelayedExpansion
set /a total=0
type NUL>"Report.csv"
(
    for /R "C:\Users\amein\Google Drive\Report BACKUP\" %%f in (*.LOG) do (
        for /f %%a in ('type "%%f"^|find /C /v  "" ') do set /a total+=%%a&echo %%f , %%~nf , %%a
    )
    echo total , !total!
)>>"Report.csv"

GOTO :EOF

编辑:已解决

@echo off
cls
setlocal EnableDelayedExpansion
set /a total=0
type NUL>Report.csv
(
echo Station Number,Date ,Done
 for /R "%userprofile%\Google Drive\Report BACKUP\" %%G in (*.LOG) do (
  for %%b in ("%%~dpG\.\..") do set station= %%~nxb
  for /f %%a in ('type "%%G"^|find /C /v  "" ') do set /a total+=%%a&echo !station!,%%~nG ,%%a
 )

 echo ,TOTAL ,!total!
)>>Report.csv

GOTO :EOF

有可能吗?我希望CSV格式的报告仅显示站号,而不显示完整路径

Station 1 10092019 100

1 个答案:

答案 0 :(得分:0)

@echo off
cls
setlocal EnableDelayedExpansion
set /a total=0
type NUL>Report.csv
(
echo Station Number,Date ,Done
 for /R "%userprofile%\Google Drive\Report BACKUP\" %%G in (*.LOG) do (
  for %%b in ("%%~dpG\.\..") do set station= %%~nxb
  for /f %%a in ('type "%%G"^|find /C /v  "" ') do set /a total+=%%a&echo !station!,%%~nG ,%%a
 )

 echo ,TOTAL ,!total!
)>>Report.csv

GOTO :EOF