Solved

hep with command for /f

Posted on 2011-09-22
14
389 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Selective Copy and overwrite 7 58
DOS command won't run from WScript.Shell 8 57
Create a specific short cut with a parameter. 2 198
Date variable in batch file 2 28
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…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

777 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