JA1
JA1

Reputation: 568

Extract after delimter in a text file with Batch

I have been searching for hours, however most of the results give examples that deal with directories. I need to read a text file to achieve this and extract after the last \ and output into a new file

Below is what my file contains in a text file

HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\yahoo.com
HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\NYU.edu
HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\openssl.org

I need to extract all the different strings after the last \ in the string. How do I go about doing this?

I tried this one but it is not working properly Extracting string after last instance of delimiter in a Batch file

This is on Windows 7 x64 SP1 and I can't install other software to achieve this either.

Upvotes: 0

Views: 69

Answers (1)

MichaelS
MichaelS

Reputation: 6042

This should work:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET inFile=C:\SomePath\someFile.txt
SET outFile=C:\SomePath\anotherFile.txt
TYPE NUL>%outFile%
FOR /F "tokens=*" %%L IN (%inFile%) DO (
    ECHO %%~nxL>>%outFile%
)

You'll have to replace inFile and outFile with proper paths. If they contain spaces, surround them with quotation marks. %outFile% will always be overwritten so make sure you don't loose your data!

Upvotes: 3

Related Questions