-1

Source file, A.txt

7895537000011
7895537000028
7895537000035
7895537000042
7895537000059
7895537000066
7895537000011
7895537000011
7895537000028
7895537000028
7895537000028
7895537000059
7895537000059

Archive destination, B.txt (I need a batch that gives me this result)

7895537000011,3
7895537000028,4
7895537000035,1
7895537000042,1
7895537000059,3
7895537000066,1

 

setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (Inventario_%loja%_%dia%-%mes%-%ano%_%hour%h%min%m%secs%s.inv) do (
    echo %%a,1 >>C:\Inventario\Inventario_%loja%_%dia%-%mes%-%ano%_%hour%h%min%m%secs%s\Inventario_%loja%_%dia%-%mes%-%ano%_%hour%h%min%m%secs%s.log
)

I tried editing the above script but it did not work very well, I do not know where to start.

Compo
  • 36,585
  • 5
  • 27
  • 39

2 Answers2

4
  • Clear environment variables
  • Read file with a for /f and store incrementing count in a var _[number]
  • Loop through vars _[number] and output count.

:: Q:\Test\2018\07\12\SO_51312947.cmd
@Echo off
Set "FileIn=SO_51312947_a.txt"
Set "FileOut=SO_51312947_b.txt"

:: clear Env vars _[]
For /f "delims==" %%A in ('set _[ 2^>Nul') Do set "%%A="

:: read file, count entries
For /f "usebackq" %%A in ("%FileIn%") Do Set /A "_[%%A]+=1"

:: output environment vars
( For /f "Tokens=1-3delims=[]=" %%A in ('Set _[') do Echo %%B,%%C
) > "%FileOut%"
Type "%FileOut%"

Sample output:

> SO_51312947.cmd
7895537000011,3
7895537000028,4
7895537000035,1
7895537000042,1
7895537000059,3
7895537000066,1
  • I like this obvious method using an [array](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990). **+1** What I don't like is the strange `_` name for the array variable. I don't understand what is the reason to use characters that makes the code look more complicated what it really is. Why just don't use a simple and clear name like `count[number]`? – Aacini Jul 13 '18 at 00:36
  • 1
    @Aacini Well a common prefix is needed and the `_` is often used to not interfere with other variable names, the numbers are quite long and not knowing the number of entries in the file a short environment variable saves memory. Also every decent text editor can rapidly exchange `_[` with whatever `Count[` –  Jul 13 '18 at 09:57
  • Welcome as a new user to [SO]. Please take the [tour]. If an answer solves your question or you find it helpful you should consider to [accept the answer](http://stackoverflow.com/help/accepted-answer) and/or [vote up](https://stackoverflow.com/help/why-vote) –  Jul 13 '18 at 11:52
0

Here is another method using sort to sort the input file, for /F and findstr to search for duplicates and find to count them:

@echo off
set "_FILE=A.txt"
set "PREV="
for /F delims^=^ eol^= %%L in ('sort "%_FILE%"') do (
    set "LINE=%%L"
    setlocal EnableDelayedExpansion
    set "SRCH=!LINE:\=\\!"
    set "LINE=!LINE:"=^""!" & rem ^"
    for /F %%K in ('findstr /X /C:"!SRCH:"^=\"!" "!_FILE!" ^| find /C "!LINE!"') do (
        endlocal
        set "CURR=%%L,%%K"
        setlocal EnableDelayedExpansion
        if not "!CURR!"=="!PREV!" echo(!CURR!
        endlocal
        set "PREV=%%L,%%K"
    )
)
aschipfl
  • 33,626
  • 12
  • 54
  • 99