For loop 如何获取文件父级';s文件夹使用批处理

For loop 如何获取文件父级';s文件夹使用批处理,for-loop,batch-file,For Loop,Batch File,Im使用下面的批处理从3个站点文件夹中的多个.LOG文件生成CSV报告。代码可以完美地生成报告,但我希望报告中也包含父文件夹(Station number文件夹而不是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 BACK

Im使用下面的批处理从3个站点文件夹中的多个.LOG文件生成CSV报告。代码可以完美地生成报告,但我希望报告中也包含父文件夹(Station number文件夹而不是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
日志文件路径:

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
(
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

请尝试将命令行
用于(“%%dpf..”中的%%I,do echo Station文件夹是:%%~fI
作为外部
的第一行,用于
,用于完整路径,
用于(“%%dpf..”)中的%%I,do echo Station文件夹是:%%~nxI
仅用于
站1
站2
。。。打开一个窗口,运行
获取/?
并阅读输出帮助。然后您还应该知道为什么
f
不是作为循环变量的最佳选择。我建议进一步将
C:\Users\amein
替换为
%UserProfile%
。提示:用
%%~dpG
引用的路径字符串始终以反斜杠结尾。由于这个原因,
%%~dpG
不应该再加上一个反斜杠和文件/文件夹路径/名称
\
在字符串
%%~dpG\.\..
中完全无用。您真正需要的是
%%~dpG..
它在执行时扩展为
C:\Users\amein\Google Drive\Report BACKUP\Station1\TAG\
,这正是您需要引用的
C:\Users\amein\Google Drive\Report BACKUP\Station1
。PS:您也可以使用
for%%b in(%%~dpG..)为/f%%a in('type“%%G.”^ |%SystemRoot%\System32\find.exe/C/v“”)是否设置/a“总计+=%%a”和echo%%~nxb、%%~nG、%%a