To remove some content from a file using a DOS batch file

I have a Text.dat file containing some unwanted data. I need to write a batch DOS file to remove unnecessary data and put it in some other file, say file "file2.dat" and leave the source file with data only. Pls Help

+6
batch-file
source share
2 answers

Instead of find I would use findstr , which is a more powerful find .

To create a file consisting only of unwanted data:

findstr "unwanted" text.dat > unwanted.dat

To create a new file from text.dat with deleted data:

findstr /v "unwanted" text.dat > text_cleaned.dat

findstr can be found online here .

Full link on the command line:

 C:\>findstr /? Searches for strings in files. FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file] [/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]] strings [[drive:][path]filename[ ...]] /B Matches pattern if at the beginning of a line. /E Matches pattern if at the end of a line. /L Uses search strings literally. /R Uses search strings as regular expressions. /S Searches for matching files in the current directory and all subdirectories. /I Specifies that the search is not to be case-sensitive. /X Prints lines that match exactly. /V Prints only lines that do not contain a match. /N Prints the line number before each line that matches. /M Prints only the filename if a file contains a match. /O Prints character offset before each matching line. /P Skip files with non-printable characters. /OFF[LINE] Do not skip files with offline attribute set. /A:attr Specifies color attribute with two hex digits. See "color /?" /F:file Reads file list from the specified file(/ stands for console). /C:string Uses specified string as a literal search string. /G:file Gets search strings from the specified file(/ stands for console). /D:dir Search a semicolon delimited list of directories strings Text to be searched for. [drive:][path]filename Specifies a file or files to search. Use spaces to separate multiple search strings unless the argument is prefixed with /C. For example, 'FINDSTR "hello there" xy' searches for "hello" or "there" in file xy 'FINDSTR /C:"hello there" xy' searches for "hello there" in file xy Regular expression quick reference: . Wildcard: any character * Repeat: zero or more occurances of previous character or class ^ Line position: beginning of line $ Line position: end of line [class] Character class: any one character in set [^class] Inverse class: any one character not in set [xy] Range: any characters within the specified range \x Escape: literal use of metacharacter x \<xyz Word position: beginning of word xyz\> Word position: end of word 
+8
source share

Use the FIND command.

 find /v "unwanted" text.dat >file2.dat 
+4
source share

All Articles