Wednesday, April 20, 2011

Copying Strings from One File to Another

The task to be discussed in this post looks very simple: copy some strings from a text file to another one. The file and the amount of strings are given. Here you can see the solution:
@echo off
setlocal enabledelayedexpansion
set "N=17"
set "count=0"
for /f "tokens=*" %%a in (first_file.txt) do (
if !count! GEQ !N! goto :EOF
echo %%a>>second_file.txt
set /a "count+=1"
)

But you can also try another version of this script. It will look like this (you should declare variables to run this script):
set count=0
for /f "skip=2 tokens=*" %%a in (%file_name%) do (
set /a count=!count!+1
if /i !count! leq N echo %%a>>new.txt
)
Hope you'll find these scripts useful.

No comments:

Post a Comment

Translate