3

I have a text file that contains names of all the files in my directory followed by its date.

Is it possible in batch/bash to extract those files names from the text file and its date based on a value on the string.

I have already checked Extracting strings from text file but this does not answer my query. I have also checked numerous others including Get specific string from a text file using batch command and seems to hit a dead end as it doesn't get all the lines from the whole text file.

a typical string/file name looks like this

5f367f72-9e2f-4f89-b3d6-2cdafed17d94_IRO_RMUSI_SONG_TimayaKingPerryy_KomKom.mxf_A1.MXF,25/07/2019 05:47:24

The identifier is IRO. i have more files in the text file as well but I want it to be able to look at the text file, find everything that has the identifier and make a new text file with the IRO file and date populated.

There are about 150,000 +/- line of string so manually doing this wont be easy.

If its possible to be done in a batch-file i would appreciate it.

Compo
  • 36,585
  • 5
  • 27
  • 39
A Person
  • 441
  • 7
  • 19

1 Answers1

3

That's quite easy if you think about it. Filter your file with findstr "_IRO_"), then split the filename from the date with a for /f loop (according to your example, they are delimited with a comma):

(for /f "delims=," %%a in ('type "names.txt" ^|findstr "_IRO_"') do @echo %%a) > "newnames.txt"

Note: to execute it directly from a command line instead of in a batch file, replace every %%a with %a.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • 1
    Hi, the output this generates is empty. It returns a new file but comes up blank. i passed the file path to the and replaced it with the names.txt – A Person Oct 01 '19 at 11:14
  • it can't be empty if the names file exists, is in the same folder as the batch file and contains at least one line containing `_IRO_`. – Stephan Oct 01 '19 at 11:18
  • If the file path and/or the file's name contains spaces, quote it like `... in ('<"c:\path to file\names.txt" findstr ...` – Stephan Oct 01 '19 at 11:19
  • 1
    Yep, it is. the names.txt in my case has 25 files names as shown above with the IRO ID – A Person Oct 01 '19 at 11:20
  • 1
    even with the quotes marks added. the output file is empty – A Person Oct 01 '19 at 11:22
  • is there a way for me to upload the sample text file? so i can show you how it looks – A Person Oct 01 '19 at 11:22
  • On the command line, do a simple ` – Stephan Oct 01 '19 at 11:23
  • Why using input redirection (`<`) with `findstr`? Let me recommend to use `findstr /L "_IRO_" "names.txt"` since `findstr` might hang in case `names.txt` is not terminated with a final line-break... – aschipfl Oct 01 '19 at 11:28
  • @aschipfl redirection is faster, but you have a point with the possible missing line-break at the end. (I'm not sure, whether `findstr "string" "file"` has the same problem, so I changed to `type "file" | findstr "string"`, as `type` adds the line-break if not present). (On the other hand - we could simply use `find` instead of `findstr`...) – Stephan Oct 01 '19 at 11:36
  • Redirection is faster? really?? I'm surprised! I'd expect the opposite. Anyway, piping does not have a problem with missing final line-break. But both redirection and piping limit the lime length to 8191 characters (see [this thread](https://stackoverflow.com/q/8844868))... – aschipfl Oct 01 '19 at 11:39
  • @aschipfl: maybe I remember incorrectly or confuse it with something else. – Stephan Oct 01 '19 at 11:45
  • @APerson I rechecked. It works at my end. Do you get any messages on the screen? – Stephan Oct 01 '19 at 11:52
  • Just tested with a 0.5 GiB file (64 Ki lines with 8190 bytes each, excl. the line-breaks): `findstr "^" "huge.txt" > nul` lasts 1.6 s on my system, `findstr "^" < "huge.txt" > nul` lasts 2.0 s; piping is hard to measure since there's another command `type` involved... – aschipfl Oct 01 '19 at 12:02
  • @Stephan, I tried it now and it work perfectly thanks – A Person Oct 01 '19 at 12:43