@ECHO OFF
SETLOCAL
:: remove variables starting $
FOR /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "outfile=%destdir%\outfile.txt"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*-fid*" '
) DO (
SET "filename=%%a"
CALL :process
)
(
FOR /F "tokens=1,2delims=$=" %%a In ('set $ 2^>Nul') DO ECHO(%%a^|%%b
)>"%outfile%"
GOTO :EOF
:process
SET "filename=%filename:*-fid=%"
FOR /f "delims=-" %%q IN ("%filename%") DO SET /a $%%q+=1
GOTO :eof
You would need to change the settings of sourcedir
and destdir
to suit your circumstances.
Produces the file defined as %outfile%
After clearing all the $
variables (for safety's sake), perform a directory listing without directorynames and in basic form of files in the source directory matching *-fid*.
For each name found, assign the name to filename
and execute the :process
routine, which first removes the characters up to and including -fid
from filename
then uses the delims=-
option to assign the part originally between -fid
and the following -
to %%q
.
set
the variable $%%q
up by 1 (if $?? is undefined, assign 1
)
Finally, when all the names have been processed, list the variables named $...
using set
which produces a report of the style
$1000=2
$2000=3
Using $
and =
as delimiters puts token 1 (eg 2000
) into %%a
and token 2 (eg 3
) into %%b
. Write these to the output using echo
, remembering to escape the pipe (|
) with a caret (^
) to suppress the interpretation as a redirector.
The parentheses around the for...$...
ensures the output is directed to the destination file specified.