Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

hep with command for /f

Posted on 2011-09-22
14
Medium Priority
?
409 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
  • +2
14 Comments
 
LVL 39

Assisted Solution

by:Aaron Tomosky
Aaron Tomosky earned 80 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 85

Assisted Solution

by:oBdA
oBdA earned 920 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 58

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 58

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 58

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 85

Accepted Solution

by:
oBdA earned 920 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 85

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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

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…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

604 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