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 )
Hi Rudasi @echo off
Rem This creates an empty file
type nul > text.txtrem 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
Perfect, that solves the problem, does using >> instead of > stop the file from being reopened or recreated? Thanks a lot.
Hi > Creates a new file, even overwriteing data if file already exists.
>> Appends data to the already open file
Hope this helps
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.
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)
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.
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.
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)
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?
not exactly. this code makes all the lines of the file into one line. f/e, file contains:
one
two
three testingmyfiles 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 /?
Yes (14) | ![]() | |
No (14) | ![]() | |
I don't know (15) | ![]() |