这就是我所拥有的:
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
@echo off
set num=0
set "suffix="
for /f "delims=" %%s in ('dir /b /a-d /o:gen') do (
if /i "%%~xs" equ "!suffix!" (
set /a num+=1
) else (
if !num! neq 0 echo "!suffix!" !num!
set num=1
set suffix=%%~xs
)
)
(
echo "%suffix%" %num%
) >>"%~dp0%date:~-4,4%-%date:~-10,2%-%date:~-7,2% Logfile.txt"
我搜索了论坛和互联网,但无果。有人能给我指点一下吗?
编辑:发现错误,附在下面以供参考。
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
@echo off
set num=0
set "suffix="
for /f "delims=" %%s in ('dir /b /a-d /o:gen') do (
if /i "%%~xs" equ "!suffix!" (
set /a num+=1
) else (
if !num! neq 0 echo "!suffix!" !num! >>"%~dp0%date:~-4,4%-%date:~-10,2%-%date:~-7,2% Logfile.txt"
set num=1
set suffix=%%~xs
)
)
答案1
您的“echo”位于for / do
- 循环之外,这就是为什么它只会打印一次。把它进入像这样的部分do
:
for /f "delims=" %%s in ('dir /b /a-d /o:gen') do (
if /i "%%~xs" equ "!suffix!" (
set /a num+=1
) else (
if !num! neq 0 echo "!suffix!" !num!
set num=1
set suffix=%%~xs
)
echo "%suffix%" %num% >>"%~dp0%date:~-4,4%-%date:~-10,2%-%date:~-7,2% Logfile.txt"
)