• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

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"
0
nappy_d
Asked:
nappy_d
  • 9
  • 7
  • 5
  • +2
2 Solutions
 
Steve KnightIT 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.

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
 
nappy_dAuthor Commented:
Thanks.  Let check each line
0
 
Steve KnightIT 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
notepad output.txt
and search for find /q /s ""



0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
paultomasiCommented:
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
 
Steve KnightIT 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
 
paultomasiCommented:
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
 
paultomasiCommented:
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
 
Steve KnightIT 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
 
paultomasiCommented:
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
 
paultomasiCommented:
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
 
Steve KnightIT 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
 
Steve KnightIT 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"

Open in new window

0
 
paultomasiCommented:
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
 
Steve KnightIT 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
 
paultomasiCommented:
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
 
Bill PrewCommented:
Could you post both the actual TXT file you are using, and the actual BAT file you are running.

~bp
0
 
nappy_dAuthor 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
 
Steve KnightIT 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
notepad output.txt

Steve
0
 
Bill PrewCommented:
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
 
Steve KnightIT ConsultancyCommented:
good point.... I had assumed the default directory, i.e. %userprofile%
0
 
nappy_dAuthor 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
 
ReneGeCommented:
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
 
ReneGeCommented:
Note that I added "/F" to force delete if file is in use. Remove "/F" if you do not wish so.
0
 
ReneGeCommented:
Just realised that Paul saw it too :)
0
 
nappy_dAuthor 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
 
nappy_dAuthor Commented:
I got my vendor to help me resolve the issue of the archived stubs
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 9
  • 7
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now