Solved

DOS for /f command deleting everything in profile when run

Posted on 2011-09-26
26
275 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
Comment Utility
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
Comment Utility
Thanks.  Let check each line
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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
 
LVL 11

Accepted Solution

by:
paultomasi earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
good point.... I had assumed the default directory, i.e. %userprofile%
0
 
LVL 32

Author Comment

by:nappy_d
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Just realised that Paul saw it too :)
0
 
LVL 32

Author Comment

by:nappy_d
Comment Utility
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
Comment Utility
I got my vendor to help me resolve the issue of the archived stubs
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now