Solved

hep with command for /f

Posted on 2011-09-22
14
368 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 38

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 83

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
 
LVL 51

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 51

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 51

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 83

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 83

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now