简体   繁体   中英

Output filename and count of a directory in a text file - windows

I have a batch to output filename and count of filenames with the same prefix and date in a text file.

Eg

Q1231111.zip

Q1241111.zip

where:

  • Q123 - is a prefix
  • 1111 - is a date.

I want an output such as:

123 : 1

124 : 1

125 : 0

But the batch file unable to output the last one. I wanna see the file is present so i need the 0 output.

Here's my code:

@echo off
setlocal EnableExtensions
for %%I in ("Z:\StoreDataJDA\Q1231111.zip") do call :CountFile "%%~nI"
for %%I in ("Z:\StoreDataJDA\Q1241111.zip") do call :CountFile "%%~nI"
for %%I in ("Z:\StoreDataJDA\Q1251111.zip") do call :CountFile "%%~nI"

for /F "tokens=2,3 delims=#=" %%I in ('set Group# 2^>nul') do echo %%I: %%J >>count.txt
endlocal
goto :EOF

:CountFile
set "FileName=%~1"
set "FileGroup=%FileName:~1,4%"
if "Group#%FileGroup%" == "" (
    set "Group#%FileGroup%=1"
) else (
    set /A Group#%FileGroup%+=1
)
goto :EOF
pause

Thanks in advance!

From your description, it's hard to tell exactly what you are looking for. But by looking at your very similar question from a year ago, and from Gerhard's comment, I think what you're looking for is:

Count the number of files with a given prefix, each file having a different date suffix.

I think this should do it:

@echo off
setlocal EnableExtensions
set "Group#123=0" & for %%I in ("Z:\StoreDataJDA\Q123????.zip") do call :CountFile "%%~nI"
set "Group#124=0" & for %%I in ("Z:\StoreDataJDA\Q124????.zip") do call :CountFile "%%~nI"
set "Group#125=0" & for %%I in ("Z:\StoreDataJDA\Q125????.zip") do call :CountFile "%%~nI"

(for /F "tokens=2,3 delims=#=" %%I in ('set Group# 2^>nul') do echo %%I: %%J)>count.txt
endlocal
goto :EOF

:CountFile
set "FileName=%~1"
set "FileGroup=%FileName:~1,3%"
set /A Group#%FileGroup%+=1
goto :EOF

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM