Showing posts with label working with text files. Show all posts
Showing posts with label working with text files. Show all posts

Friday, September 9, 2011

Removing Unnecessary Trailing Spaces

Imagine: we have a variable with some string data, and we have to remove unnecessary spaces from the end of this variable. The code below shows how to solve this problem:

@echo off
setlocal
set "a=111 222 "

:loop
set /a n+=1
for /f "tokens=%n%" %%i in ("%a%") do (
if not "%%i"=="" set b=%b%%%i && goto:loop
)

set a=%b:~0,-1%
set b=
echo "%a%"


Of course, you have to change this code a bit before using it. You can do it with the help of our award-winning batch files editor. Hope you'll find this example useful.

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.

Tuesday, March 29, 2011

Adding Quotes to Lines in Text

Let's discuss quite a simple task today. Imagine: you need to change each line in a text file by adding quotes to it. I mean you want to get from list like listitem1, listitem2, listitem3... list like "listitem1", "listitem2", "listitem3". This is quite a simple task, and the script solution looks like this:
@echo off
for /f "usebackq tokens=*" %%c in ("about.txt") do (
echo "%%c",>> about1.txt
)

As far as you can see, it's not difficult to write this script with help of our award-winning batch files editor. Hope you'll find this small example useful.

Translate