hep with command for /f

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)
LVL 32
nappy_dThere are a 1000 ways to skin the technology cat.Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
oBdAConnect With a Mentor Commented:
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
 
Aaron TomoskyConnect With a Mentor SD-WAN SimplifiedCommented:
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
 
oBdAConnect With a Mentor Commented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
nappy_dThere are a 1000 ways to skin the technology cat.Author Commented:
Thanks oBda. I'll give that snippet a go this morning...

@aarontomosky...even tho invested, I will try yours too.
0
 
Bill PrewCommented:
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
 
nappy_dThere are a 1000 ways to skin the technology cat.Author Commented:
@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
 
Bill PrewCommented:
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
 
nappy_dThere are a 1000 ways to skin the technology cat.Author Commented:
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
 
Bill PrewCommented:
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
 
nappy_dThere are a 1000 ways to skin the technology cat.Author Commented:
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
 
nappy_dThere are a 1000 ways to skin the technology cat.Author Commented:
Thanks everyone!
0
 
nappy_dThere are a 1000 ways to skin the technology cat.Author Commented:
actually it worked but then it deleted everything from my profile!!!
0
 
oBdACommented:
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
 
Paul TomasiCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.