2

I need to clean up strings like this (where I read in a group of image names), where I only want the first two strings (currently I'm using, in Windows 7 cmd line - dir /a/b/p > textfile.txt):

  • Acaena inermis no barbs dbot_25Dec15_40.JPG
  • Coprosma Taiko PB121944 invbot.rs.JPG
  • Cortaderia richardii InvBot P6260038.JPG
  • Anemanthele lessoniana LIC.nestmaker.CC BY-SA 2.0.jpg
  • Myosotidium hort ibot PB109882 sqr rs.JPG

to look like this (Word space word and strip the rest):

  • Acaena inermis
  • Coprosma Taiko
  • Cortaderia richardii
  • Anemanthele lessoniana
  • Myosotidium hort

Is there a way using either cmd or batch to simplify this? Normally there would be 15 files parsed each time I do this. I'm hardly a cmd-line guru!

nigelc
  • 157

2 Answers2

2

Try this from command line:

for /F "tokens=*" %g in (textfile.txt) do @for /F "tokens=1,2" %G in ("%~ng") do @if not "%H"=="" echo(%G %H

or start from scratch

for /F "tokens=*" %g in ('dir /A/B/S') do @for /F "tokens=1,2" %G in ("%~ng") do @if not "%H"=="" echo(%G %H

You need to double % percent signs in for loop variable names in a batch file as follows:

@echo OFF
for /F "tokens=*" %%g in (textfile.txt) do (
    for /F "tokens=1,2" %%G in ("%%~ng") do if not "%%H"=="" echo(%%G %%H
)

or

@echo OFF
for /F "tokens=*" %%g in ('dir /A/B/S') do (
    for /F "tokens=1,2" %%G in ("%%~ng") do if not "%%H"=="" echo(%%G %%H
)

To redirect output to a plain text file taxons.txt (note additional () parentheses:

>taxons.txt (for /F "tokens=*" %g in ('dir /A/B/S') do @for /F "tokens=1,2" %G in ("%~ng") do @if not "%H"=="" echo(%G %H)

or in a batch script:

@echo OFF
>taxons.txt (
for /F "tokens=*" %%g in ('dir /A/B/S') do (
    for /F "tokens=1,2" %%G in ("%%~ng") do @if not "%%H"=="" echo(%%G %%H
)
)

Resources (required reading):

JosefZ
  • 13,855
1

Download the free text editor called Notepad++

Open your textfile.

Press CTRL-H to open the find and replace dialog.

At the bottom left, check Regular Expression

In Find: enter ^(.+?[ ].+?)[ ].+$ In Replace: enter $1

This code will assume that after the 2nd word a space is present. If there is another char there, such as a _ or - replace the second [ ] with [ _-] (listing whatever char there is). Keep in mind that if you have this text part as the word too, it will cut off there in other searches.

LPChip
  • 66,193