Solved

hep with command for /f

Posted on 2011-09-22
14
394 Views
Last Modified: 2012-05-12
Been a long time since had to do this but it's the fastest way to get done what I need.

I have a text file with a 1000 lines of files that I need to delete and the only way that is working is this Windows command line.

I know I could use the for /f command but cannot remember how to write this.

I think its for /f IN c:\file.txt del /q/s

Is that right?(Just need a second set of eyes)
0
Comment
Question by:nappy_d
  • 6
  • 3
  • 3
  • +2
14 Comments
 
LVL 39

Assisted Solution

by:Aaron Tomosky
Aaron Tomosky earned 20 total points
ID: 36584866
Add the parameter before in
http://ss64.com/nt/for.html
So I think it is:
 for /f %%filename IN c:\file.txt del %filename /q/s

Untested
0
 
LVL 84

Assisted Solution

by:oBdA
oBdA earned 230 total points
ID: 36584893
That's not completely correct.
Try this in a command prompt:
for /f "delims=" %a in (C:\file.txt) do del /q /s "%~a"

Open in new window

To use it in a batch script, use this:
for /f "delims=" %%a in (C:\file.txt) do del /q /s "%%~a"

Open in new window

0
 
LVL 32

Author Comment

by:nappy_d
ID: 36586146
Thanks oBda. I'll give that snippet a go this morning...

@aarontomosky...even tho invested, I will try yours too.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 53

Expert Comment

by:Bill Prew
ID: 36586264
You'll find this out with testing, but oBdA has the correct syntax.

This assumes that if the TXT file doesn't have full path information, then you must be in the folder that contains the files.

~bp
0
 
LVL 32

Author Comment

by:nappy_d
ID: 36586318
@aarontomosky; your command kicks out this error message
C:\Users\Administrator\...>for /f %%filename IN "C:\<pathToMyFiletag\Remover\adp.txt" del %filename /q /s >"C:\<pathToMyFile\tagRemover\adpDel.txt"
%%filename was unexpected at this time.


oBda; your command just deletes my file I was looking for it to read the file contents from the first snippet
The second snippet tells me that %%a was unexpected at this time
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 36586757
oBdA's commands look right to me, are you sure you typed (or better yet cut and pasted) exactly as written?

It sounds like you tried to run the second one from a command prompt, but it will only work INSIDE a BAT file.  The first version (notice that %a is changed to %%a) is the form to use directly from a command promt.

~bp
0
 
LVL 32

Author Comment

by:nappy_d
ID: 36587106
So this is the full command I entered based you your response.  I still get the same error


C:\Users\Administrator\Desktop\PoINT Storage Manager\tagRemover>for /f "delims=" %%a in ("C:\Users\Administrator\Desktop\PoINT Storage Manager\tagremover\adp.txt") do del /q /s "%%~a"

Open in new window

0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 36587713
Ah, you needed to quote the file name!  In that case adjust as follows (this is the from the command line version).  If running in a BAT file change the %a to %%a in 2 places).

for /f "usebackq delims=" %a in ("C:\Users\Administrator\Desktop\PoINT Storage Manager\tagremover\adp.txt") do del /q /s "%~a"

Open in new window

~bp
0
 
LVL 32

Author Comment

by:nappy_d
ID: 36588132
Usebackq? What is that?

Also when using this command it is attemping to delete the contents of my profile(not a big deal as I can recreate it)

Yes, I copeid and pasted exactly as you entered it.
0
 
LVL 84

Accepted Solution

by:
oBdA earned 230 total points
ID: 36591044
In a "for /f" loop a text enclosed in double quotes will be treated as a literal string, not as a file name. To work around that, you can either use the "usebackq" option or (what I prefer out of habit, because it prevented some issues with Unicode files in NT4 as well) use a "type" command.
And an easy way to test batch commands is to just echo out the line that would othewise be run.
So here's the test version (command line, NOT batch):
for /f "delims=" %a in ('type "C:\Users\Administrator\Desktop\PoINT Storage Manager\tagremover\adp.txt"') do (ECHO del /q /s "%~a")

Open in new window

And here's the "live" version (again command line, not batch):
for /f "delims=" %a in ('type "C:\Users\Administrator\Desktop\PoINT Storage Manager\tagremover\adp.txt"') do (del /q /s "%~a")

Open in new window

0
 
LVL 32

Author Closing Comment

by:nappy_d
ID: 36591119
Thanks everyone!
0
 
LVL 32

Author Comment

by:nappy_d
ID: 36591122
actually it worked but then it deleted everything from my profile!!!
0
 
LVL 84

Expert Comment

by:oBdA
ID: 36591155
That shouldn't have happened.
Were there empty lines (with only a space or so in them) in that file?
Was one of the file names in the file actually a folder name?
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36591227
Or you could try:
for /f "tokens=* usebackq" %a in ("C:\Users\Administrator\Desktop\PoINT Storage Manager\tagremover\adp.txt") do del /q /s "%~a"

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question