Solved

DOS for /f command deleting everything in profile when run

Posted on 2011-09-26
26
276 Views
Last Modified: 2012-08-14
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"
0
Comment
Question by:nappy_d
  • 9
  • 7
  • 5
  • +2
26 Comments
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 125 total points
ID: 36598772
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.

You could add instead:

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

Author Comment

by:nappy_d
ID: 36599586
Thanks.  Let check each line
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36599631
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
notepad output.txt
and search for find /q /s ""



0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 11

Accepted Solution

by:
paultomasi earned 125 total points
ID: 36602383
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:


   notepad files.txt


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

Expert Comment

by:Steve Knight
ID: 36602438
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
 
LVL 11

Expert Comment

by:paultomasi
ID: 36707695
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
 
LVL 11

Expert Comment

by:paultomasi
ID: 36707755
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"

Open in new window


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

Open in new window

NOTE: For batch files, use %%a instead of just %a.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36708006
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
 
LVL 11

Expert Comment

by:paultomasi
ID: 36708278
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
 
LVL 11

Expert Comment

by:paultomasi
ID: 36708284
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36708310
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36708321
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"

Open in new window

0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36714303
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"
)

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36714643
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
 
LVL 11

Expert Comment

by:paultomasi
ID: 36714932
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...

nappy_d, please clarify these points.
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 36716361
Could you post both the actual TXT file you are using, and the actual BAT file you are running.

~bp
0
 
LVL 32

Author Comment

by:nappy_d
ID: 36720455
I would but I've got two files with 600 lines each.  I am slowly checking line by line as I get time ...
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36720487
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
notepad output.txt

Steve
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 36754777
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36812852
good point.... I had assumed the default directory, i.e. %userprofile%
0
 
LVL 32

Author Comment

by:nappy_d
ID: 36971166
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
 
LVL 10

Expert Comment

by:ReneGe
ID: 36971245
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"

Open in new window

0
 
LVL 10

Expert Comment

by:ReneGe
ID: 36971269
Note that I added "/F" to force delete if file is in use. Remove "/F" if you do not wish so.
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 36971278
Just realised that Paul saw it too :)
0
 
LVL 32

Author Comment

by:nappy_d
ID: 37404108
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
 
LVL 32

Author Closing Comment

by:nappy_d
ID: 37404115
I got my vendor to help me resolve the issue of the archived stubs
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 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