-2

I have around 600 files of different stores in a folder. Their name is like StoreNum_MMDD.txt/Txt/tXt/TxT/TXT. I want to find all unique StoreNum.

Please note the extension, the command should search for all possible combinations.

For eg. If 4 files like

 123_0221.txt 
 32145_1220.txt 
 123_1020.TXT 
 455_0412.txT

then my output should be

123  
32145  
455

This is my code:

 FOR /F "tokens=1 delims=_ " %%i in (%FILE%) do ( echo %%i ) 

But this is generating all

123
32145
123
455

where the "123" is repeated

Magoo
  • 77,302
  • 8
  • 62
  • 84
ckp
  • 11
  • 1
  • 2
  • 2
    You've done a great job of defining your requirements. What have you done so far to try to actually implement them? We're not a code writing service, where you post what you need and someone here churns out code to meet that need. If that's what you're looking for, hire a contractor to write your code for you. We're more than willing to *help*, but we're not doing everything for you. – Ken White Feb 01 '16 at 18:26
  • Hi Ken, I have tried this FOR /F "tokens=1 delims=_ " %%i in (%FILE%) do ( echo %%i ) But this is generating all 123 32145 123 455 – ckp Feb 01 '16 at 18:32

1 Answers1

1
@echo off
setlocal

rem Process all files and create *unique* array elements per store
for /F "delims=_" %%i in ('dir /B *.txt') do set "store[%%i]=1"

rem Show *subscripts* of elements in "store" array (not the value, that is always 1)
for /F "tokens=2 delims=[]" %%i in ('set store[') do echo %%i

For further details on array management in Batch files, see: Arrays, linked lists and other data structures in cmd.exe (batch) script

If you have doubts about the for command, review the suggested link.

If you want to create an output file, enclose the whole last for command in parentheses and use a (redirection to an) > output file.

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Personally, had the cowboys not got in first, I'd have used `if not defined store[%%i} set store[%%i]=1&echo %%i` in place of your first `set` line, making the second `for` redundant. – Magoo Feb 01 '16 at 20:31