newline in txt file with for loop

May 14, 2010 at 13:00:08
Specs: Windows XP
Hi,

I am using a forloop to look through a file with paths and then output the filename only using basename. I want to output all the names to a txt file. SO far I am only able to output the last name. Here is my code,

@echo off

rem get files into  text file

set PWD=%CD%
set WWW=%1
set WKD=%WWW%

CD %WKD%

FOR /F "tokens=*" %%a in (testing2.txt) do (

basename %%a  > text.txt 
echo. > text.txt

)


See More: newline in txt file with for loop

Report •

#1
May 14, 2010 at 13:40:26
Hi Rudasi

@echo off

Rem This creates an empty file
type nul > text.txt

rem get files into text file

set PWD=%CD%
set WWW=%1
set WKD=%WWW%

CD %WKD%

FOR /F "tokens=*" %%a in (testing2.txt) do (

basename %%a > >text.txt
echo. > >text.txt


Report •

#2
May 14, 2010 at 13:46:25
Perfect, that solves the problem, does using >> instead of > stop the file from being reopened or recreated? Thanks a lot.

Report •

#3
May 14, 2010 at 14:55:06
Hi

> Creates a new file, even overwriteing data if file already exists.
>> Appends data to the already open file
Hope this helps


Report •

Related Solutions

#4
May 14, 2010 at 15:03:40
Thanks once again. I have one last question. The data that I get in the text file now has a funny box at the end. I read some thread and from there I saw that

sed -e "s/[^ -~]//g" text.txt > text1.txt

removes the box, but this adds a blank row to each row of data in text1.txt. Do you know how i can remove the blank lines?

Thanks.


Report •

#5
May 14, 2010 at 16:19:32
Hi
As a For loop ignores blank lines, does this help.

@echo off
type nul > text2.txt
for /f "tokens=*" %%a in (text1.txt) do (echo %%a >>text2.txt)


Report •

#6
May 17, 2010 at 06:37:20
Hi,

Thanks for your reply, that doesnt seem to work, that will create an echo is off message in place of the blank line in text2.txt.


Report •

#7
May 17, 2010 at 06:51:29
This is what I have so far,

@echo off

rem get files into  text file

set PWD=%CD%
set WWW=%1
set WKD=%WWW%

CD %WKD%

FOR /F "tokens=*" %%a in (testing2.txt) do (

basename %%a >> text.txt 
echo. >> text.txt 
)

sed -e "s/[^ -~]//g" text.txt > text1.txt

type nul > text2.txt
For /F "tokens=* " %%b in (text1.txt) Do (Echo %%b >>text2.txt)

testing2.txt is athe file with all the pathnames I want to shorten.


Report •

#8
May 17, 2010 at 17:40:12
might try this instead of all of the above:
@echo off>test.txt
for /f "tokens=*" %%a in (testing2.txt) do echo %%~nxa
>>test.txt
::----- end
or to skip testing2.txt and get direct from dir:

@echo off>test.txt
for %%a in (*.*) do echo %%~nxa >> testing.txt
::---- end
(I don't have "basename" command on mine)


Report •

#9
May 18, 2010 at 10:07:02
Thanks for your reply, that works to, would any one know what this does?

FOR /F "tokens=*" %%b in (testing.txt) do (

  set MYFILES=!MYFILES! %%b.c

)

My understanding is that it loops through testing.txt and adds a .c to everything. Is they a better way to do this and how do i use MYFILES, anyway to output it to a file?


Report •

#10
May 18, 2010 at 10:46:05
not exactly. this code makes all the lines of the file into one line. f/e, file contains:
one
two
three testing

myfiles ends up:
one.c two.c three testing.c
because of the accumulation onto var. MYFILES (ie: MYFILES=MYFILES+NEXTLINE)
suggest viewing the cmdline helps:
for /?
set /?
if /?
setlocal /?


Report •

Ask Question