How to remove certain text from text file?

April 27, 2011 at 15:57:51
Specs: Windows Vista
So I needed to copy all the file & folder names from a directory to save into a text file. I did so by using "DIR /B /O:N > filename.txt" in command prompt.

Not only did it save all the file names inside the folders, but it added the directory location before all the names.

1. Is there someway to only save JUST the names of each file & folder inside that directory, without it saving the file names in each folder?

2. Is there someway that after the text file is created, to somehow remove the directory location before each name in the text? Can a script do this?

Thanks,
-Rich


See More: How to remove certain text from text file?

Report •


#1
April 27, 2011 at 23:00:44
Can you show a sample of what you have and how it should look after being processed?

Report •

#2
April 27, 2011 at 23:31:47
"Not only did it save all the file names inside the folders, but it added the directory location before all the names."

Not obvious how.

Moving right along...

You can lose some of the condusion by not flip-flopping between folder & directory.


=====================================
Life is too important to be taken seriously.

M2


Report •

#3
April 28, 2011 at 00:25:44
Judago, heres the example of what I have:

C:\Users\Rich\Desktop\Movies\Weeds Season 2 Complete\Torrent downloaded from Demonoid.com.txt
C:\Users\Rich\Desktop\Movies\Weeds Season 2 Complete\Weeds.S02E01.HDTV.XviD-LOL.avi
C:\Users\Rich\Desktop\Movies\Weeds Season 2 Complete\Weeds.S02E02.HDTV.XviD.PROPER-hV.avi
C:\Users\Rich\Desktop\Movies\Weeds Season 2 Complete\Weeds.S02E03.HDTV.XviD-XOR.avi

Notice it shows all the files inside the "Weeds Season 2 Complete" folder, when I would only like just the folder name ("Weeds Season 2 Complete") and not all the files inside. (There must be a command to add to "DIR /B /O:N > filename.txt" in the command prompt?)

Back to the reason of the topic; Is there someway (script maybe?) to remove the directory location text? Meaning to remove all the "C:\Users\Rich\Desktop\Movies\" before each line.

This way I only have just the names of the folders (and some file names) that are inside the 'Movies' folder.


I hope I cleared this up enough.
Thanks.

P.S. Mechanix2Go - Sorry, but I'm not really understanding what you mean.


Report •

Related Solutions

#4
April 28, 2011 at 01:26:26
The reason I asked to see both what you have and what you want it to look like is because I was(and still am) confused as to the ultimate aim.


Do you just want the whole listing to look like:

Weeds Season 2 Complete\Weeds.S02E03.HDTV.XviD-XOR.avi


Report •

#5
April 28, 2011 at 02:00:56
I want it to look like this:

Weeds Season 2 Complete
Another Movie Folder Name Here
And Another Movie Folder Name Here
And Even Another Movie Folder Name Here
and so on

*Another Movie Folder Name Here* would be the name of a movie's folder. Because everything in the directory I want the names from are all movies, so I just want all the movie names saved to a text file like I have done, but I want it without the directory location and without all the files that may be contained inside the folders.

Hope this makes sense.


Report •

#6
April 28, 2011 at 02:52:06
Ok ;) That should be fairly simple, it will be easier to just regenerate the list:

Option 1:

(for /d %%a in ("C:\Users\Rich\Desktop\Movies\*") do @echo %%~na) > somefile.txt

Option 2:

pushd "C:\Users\Rich\Desktop\Movies"
dir /ad/on/b > somefile.txt
popd

Option 3:

(for /f "delims=" %%a in ('dir /b/on/ad "C:\Users\Rich\Desktop\Movies" ') do @echo %%~na) > somefile.txt

If you want to run Option 1 or Option 3 from the command line instead of a .bat script replace "%%" with "%" (half the percentage signs).


Report •

#7
April 28, 2011 at 03:06:49
Sorry but I'm not advanced in this. Do I type those commands just as you typed them, into the command prompt?

If so, I ran into this: %%a was unexpected at this time.

Edit: I tried option 1 with half the percentage signs, and it seemed to work (no errors or anything in command prompt) but somefiles.txt isn't in the folder.

I tried option 3 and I got "in was unexpected at this time"


Report •

#8
April 28, 2011 at 03:11:44
A quirk of batch scripting is that for variables have a single percentage sign on the command line and two in a script:

just copy and past this into the command prompt:

(for /d %a in ("C:\Users\Rich\Desktop\Movies\*") do @echo %~na) > somefile.txt

Edit: I forgot the "@".


Report •

#9
April 28, 2011 at 03:15:50
Are you sure the file isn't created?

It should be the exact name it is output to "somefile.txt", no "s".

Also I forgot the @.


Report •

#10
April 28, 2011 at 03:22:15
(for /d %a in ("C:\Users\Rich\Desktop\Movies\*") do @echo %~na) > somefile.txt

It responds back with "More?" lol what does this mean?


Report •

#11
April 28, 2011 at 03:28:12
It normally asks for "more?" when there is unmatched parens or quotes, this looks fine....


I think I have turned this into quite a mess ;) lets try to fix this simple issue.

Try this and see if it outputs what you want:

dir /b/on/ad "C:\Users\Rich\Desktop\Movies"

;)


Report •

#12
April 28, 2011 at 03:38:59
That worked perfect. Just a matter of copy&paste from command prompt. Thank you!

It listed all the folder names inside my "Movies" folder. It didn't list the few files (.avi and .txt) that were in the folder but that's alright, theres only a couple of those.


Thank you, I appreciate all your time and help!


Report •

#13
August 14, 2011 at 20:41:52
you can do this too

dir /b/on/ad "C:\Users\Rich\Desktop\Movies" >> text.txt


Report •


Ask Question