# DOS for /f command deleting everything in profile when run

I had some great help from fellow EE experts.  The probem is, that when the command is run, it not old deletes all the files but also all the data from the logged in profile.

This is the command I ran:
for /f "tokens=* usebackq" %a in ("C:\Users\archiver\Desktop\tagRemover\file.txt") do del /q /s "%~a"

This is a sample of the may lines I have in the text file for deletion(note: They all have quotations around the file path in my file.txt):
"\\ServerName\clients\ClientName\Accounts\2004\2003\Television 2003\Brilliance_1065_30E.mpg"
LVL 32
There are a 1000 ways to skin the technology cat.Asked:
###### Who is Participating?

Commented:
As dragon-it has pointed out, using ECHO and redirecting output to a file (FILES.TXT), you can then look at the entries in this file to confirm whether or not the DEL commands are what you expect them to be. Try this:

for /f "tokens=* usebackq" %a in ("C:\Users\archiver\Desktop\tagRemover\file.txt") do echo del /q /s "%~a">files.txt

Don't be worried about blank lines in file.txt - FOR /F simply skips them...

Also, don't be worried about your lines in file.txt containing double-quotes around them - the %~a strips them away and then they are re-added with "%~a". This is a failsafe method.

Once you've entered the above command, open FILES.TXT with Notepad like this:

and confirm all is as it should be.

Once you are satisfied all is well, remove the ECHO and the redirection restoring the command to just:

for /f "tokens=* usebackq" %a in ("C:\Users\archiver\Desktop\tagRemover\file.txt") do del /q /s "%~a"

and all should be well.

0

IT ConsultancyCommented:
Add the word "echo" in front of the del command and it will show you instead of doing it.  I imagine you will find there is a blank line in the  file.txt in which case the command will be:

del /q /s ""

which deletes everything in the current dir... which is likely to be the current user's profile.

do if not "%%~a"=="" del /q /s "%%~a"

I presume you know you can mak this a batch file by adding %% instead of % ?

Steve

0

There are a 1000 ways to skin the technology cat.Author Commented:
Thanks.  Let check each line
0

IT ConsultancyCommented:
It could also be that you have some dodgy character in the filenames there such as &  () > < etc. though less likely I imagine.

Do the echo test and see.
If you do:

(for /f "tokens=* usebackq" %a in ("C:\Users\archiver\Desktop\tagRemover\file.txt") do echo del /q /s "%~a" )>output.txt
and search for find /q /s ""

0

IT ConsultancyCommented:
Paul - surely that will only list the last line in files.txt, which is why I redirected the whole for command in one go (or could use >> on the del command I suppose).

Steve

This file:

{
test #
test3#
#
test3#
#
}

produces this output.  The #'s are mine added afterwards.... in other words lines with a space on say = fail.

del /q /s "test "
del /q /s "test3"
del /q /s ""
del /q /s "test3"
0

Commented:
Oops, Steve (dragon-it), you're right. it should be '>>' not '>'. Silly of me not to realise my own mistake. It should be:

for /f "tokens=* usebackq" %a in ("C:\Users\archiver\Desktop\tagRemover\file.txt") do echo del /q /s "%~a">>files.txt

Are you sure about blank lines in FILE.TXT - HIS file containing a list of files he wants to delete? My understanding is, if there are blank lines in this file then FOR /F does not process them. For example:

----FILE.TXT (Contains 2 blank lines: 3 and 5)----
"\\ServerName\clients\ClientName\Accounts\2004\2003\Television 2003\Brilliance_1065_30E.mpg"
"\\ServerName\clients\ClientName\Accounts\2004\2003\Television 2003\Brilliance_1065_30F.mpg"

"\\ServerName\clients\ClientName\Accounts\2004\2003\Television 2003\Brilliance_1065_30G.mpg"

"\\ServerName\clients\ClientName\Accounts\2004\2003\Television 2003\Brilliance_1065_30H.mpg"
----------------------------------------------------------

----Output Produced----
del /q /s "\\ServerName\clients\ClientName\Accounts\2004\2003\Television 2003\Brilliance_1065_30E.mpg"
del /q /s "\\ServerName\clients\ClientName\Accounts\2004\2003\Television 2003\Brilliance_1065_30F.mpg"
del /q /s "\\ServerName\clients\ClientName\Accounts\2004\2003\Television 2003\Brilliance_1065_30G.mpg"
del /q /s "\\ServerName\clients\ClientName\Accounts\2004\2003\Television 2003\Brilliance_1065_30H.mpg"
----------------------------

0

Commented:
nappy_d

I would suspect your problem lies with the contents of your FILE.TXT.

check to make sure ONLY files are being deleted if that's your intention.

Also, you could incorporate a conditional-IF statement like this:
``````FOR /F "TOKENS=* USEBACKQ" %a IN ("C:\Users\archiver\Desktop\tagRemover\file.txt") DO IF /I "%~xa"==".mp3" DEL /Q /S "%~a"
``````

How about making it a simple batch file - saves time having to type in long lines which may lead to errors...
``````@echo off
for /f "tokens=* usebackq" %%a in ("C:\Users\archiver\Desktop\tagRemover\file.txt") do (
if /i "%%~xa"==".mp3" del /q /s "%%~a"
)
``````
NOTE: For batch files, use %%a instead of just %a.
0

IT ConsultancyCommented:
Yes that's what I meant... or what I always thought was the more efficient way a I suggested in: http:#36599631

Blank lines will not process with for as you say BUT, for instance one with a single space or the like will and with the same results ultimately.

One with a just a . would have the same effect I guess too being then

del /q /s "."

Steve
0

Commented:
Ouch! I don't like the thought of 'DEL /Q /S "."' - not the sort of mistake I take too kindly to!!

Yep, a single space (or more) will result in DEL /Q /S ""

And a '.' on it's own will result in DEL /Q /S "."

And BOTH of these will delete EVERYTHING in the folder. Very scary !!
0

Commented:
I would suggest using an IF-conditional statement just in case... (assuming all he wants to do is delete MP3 files) ie,

:
if /i "%~xa"==".mp3" del "%~a"
:
0

IT ConsultancyCommented:
Or if they are all .mpg files say then run them through a find for ".mpg" or look for "\\" in each line perhaps:

for /f "tokens=* usebackq" %a in ('find "\\" "C:\Users\archiver\Desktop\tagRemover\file.txt"') do echo del /q /s "%~a"

Steve
0

IT ConsultancyCommented:
Or maybe include * too:

``````@echo off
set file="C:\Users\archiver\Desktop\tagRemover\file.txt"
REM Loop down all entries in this file making sure there IS a \\ in the line and there is NOT a wildcard *
for /f "tokens=* usebackq" %a in ('find "\\" %file% ^| find /v "*"') do del /q /s "%~a"
``````
0

Commented:
nappy_d

Just realised, have you tried the DEL command without the /S option? - Like this:

``````@echo off
for /f "tokens=* usebackq" %%a in ("C:\Users\archiver\Desktop\tagRemover\file.txt") do (
if /i "%%~xa"==".mp3" del /q "%%~a"
)
``````
0

IT ConsultancyCommented:
ouch.... Yes.... Not sure how that would do anything though except also try and delete the same filename under the same subdir (which may habe been intentional?).... Good catch and dont know why I missed it!
0

Commented:
Another thing I noticed is that the example UNC filename given is a '.MPG' file - not a '.MP3' file as I mistook from the following:

"\\ServerName\clients\ClientName\Accounts\2004\2003\Television 2003\Brilliance_1065_30E.mpg"

However, I assumed from this that all the entries in FILE.TXT are filenames rather than a mix of both filenames and foldernames.

This means my earlier suggestion of using a conditional-IF statement in the FOR-loop may not be as valid as I first thought...

0

Commented:
Could you post both the actual TXT file you are using, and the actual BAT file you are running.

~bp
0

There are a 1000 ways to skin the technology cat.Author Commented:
I would but I've got two files with 600 lines each.  I am slowly checking line by line as I get time ...
0

IT ConsultancyCommented:
Well try my option above that checks there is a \\ in each line and no * and that should cover it.

If you want to know if there are any lines that don't have \\ in then try this:

@echo off
(echo Lines without \\ in :
find /n /v "\\" yourfile.txt
echo Lines with a * in:
find /n "*" yourfile.txt
) > output.txt

Steve
0

Commented:
Just curious , when you say"The probem is, that when the command is run, it not old deletes all the files but also all the data from the logged in profile." what exactly is the folders or files that were removed?

~bp
0

IT ConsultancyCommented:
good point.... I had assumed the default directory, i.e. %userprofile%
0

There are a 1000 ways to skin the technology cat.Author Commented:
The end of the batch removed everything that was not in use in my logged on profile.  Unfortunately due to time, I had to shelve this and manually delete the files(correction, an intern did it).
0

Commented:
Assuming that in the "C:\Users\archiver\Desktop\tagRemover\file.txt", you have the list of all the files (with their full path), you wish to delete, remove the /S from your command line.

If you want to use your command line from within a batch file, replace % by %%.

Cheers,
Rene
``````for /f "tokens=* usebackq" %a in ("C:\Users\archiver\Desktop\tagRemover\file.txt") do del /f /q "%~a"
``````
0

Commented:
Note that I added "/F" to force delete if file is in use. Remove "/F" if you do not wish so.
0

Commented:
Just realised that Paul saw it too :)
0

There are a 1000 ways to skin the technology cat.Author Commented:
Sorry for this long delay, but after many MANY months, I got my vendor to resolve this issue as their archiving software had orphaned the stub files. uggh!
0

There are a 1000 ways to skin the technology cat.Author Commented:
I got my vendor to help me resolve the issue of the archived stubs
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.