批处理如何获取每个文件夹里第一个和最后一个文件的文件名,以及文件总数量并将统计结果按格式输出到txt

100分高分悬赏!因工作的特别要求,有1000个文件夹,每个文件夹里有几十个文件且都没有子文件夹没有二级目录,跪求一个批处理程序,实现批量统计这1000个文件夹的以下信息并按以下格式列到一个txt文本里。

(文件夹名)空格(文件夹第一个文件的文件名)空格(文件夹最后一个文件的文件名)空格(这个文件夹里的文件数量)

如果太抽象的话 我举个例子,比如其中一个文件夹,文件夹名是ABC,文件夹里有文件0001.jpg 0002.txt 0003.zip …… 0041.doc共41个文件,经过批处理后,其在批处理后生成的文本文件里属于它的那一行内容为:

ABC 0001.jpg 0041.doc 41

大师们,休怪我的要求奇葩,的确是工作需要,不到万不得已我真的不想问这么无厘头的问题。
但是这种批处理程序对高手来说,是易如反掌吧?

批处理文件放那1000文件夹并列位置运行,(或还可将此总文件夹拖到批处理文件图标上运行)结果在批处理文件同位置--统计.txt

@echo off
set "ds=%~dp0"
if not "%1"=="" set "ds=%~1"
cd/d "%ds%"
set n=0&set f1=-&set "fn=-"
(for /f "delims=" %%i in ('dir/b/ad')do (
  setlocal enabledelayedexpansion
  cd "%%i"
  >con echo %%i:
  for /f "delims=" %%j in ('dir/b/a-d/on')do (
     set/a n+=1
     set "fn=%%j"
     >con echo;     %%j
     if "!f1!"=="-" set f1=%%j)
     echo %%i !f1! !fn! !n!
   endlocal
))>"%~dp0统计.txt"
echo;&echo 统计完毕!
ping /n 5 127.1>nul

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-05-08
/*&cls
@echo off
set "netpath=%systemroot%\Microsoft.NET\Framework"
for /f "delims=" %%a in ('dir /ad /b "%netpath%\v?.*"') do (
if exist "%netpath%\%%a\csc.exe" (
set "cscpath=%netpath%\%%a\csc.exe"
goto :0
)
)
echo;未安装.Net Framework 2.0及其上版本组件或相关程序丢失&pause&exit
:0
>"%tmp%\$" more +25 "%~f0"
"%cscpath%" /out:"%tmp%\$Expdir.exe" "%tmp%\$"
(for /f "delims=" %%a in ('dir /ad /b') do (
setlocal enabledelayedexpansion
for /f "delims=" %%b in ('%tmp%\$Expdir.exe "%~dp0%%a"') do (
set /a n+=1
if !n! equ 1 set "#1=%%~nxb"
set "#n=%%~nxb"
)
if defined n if !n! equ 1 (echo;%%a !#1! !n!) else echo;%%a !#1! !#n! !n!
endlocal
))>"%~dp0结果.txt"
pause&exit
*/
using System;
using System.IO;
using System.Runtime.InteropServices;
class Expdir
{
[DllImport("Shlwapi.dll", CharSet = CharSet.Unicode)]
private static extern int StrCmpLogicalW(string psz1, string psz2);
static void Main(string[] args)
{
String path = args.Length>0?args[0].ToString():Environment.CurrentDirectory;
string[] files = Directory.GetFiles(path);
Array.Sort(files, StrCmpLogicalW);
foreach (string file in files)
{
Console.WriteLine(file);
}
}
}本回答被提问者采纳
相似回答