How to silently delete files with a bat file - batch-file

del /s .jpg
deletes all .jpgs .. but the problem is: it shows, in cmd when executed =>
C:\blabla..\this.jpg is deleted..
I want to turn this off. Such that user will not know what is happening (i.e, what files are being deleted).

Turn echo off to suppress showing the command being run, and redirect output to null as #Sico suggested.
#echo off
del /s *.jpg >nul 2>&1
You should see nothing displayed when the bat file is run.

In jammykam's answer, he uses >nul 2>&1. What this does is redirect both standard output and standard error to the null device. However, hiding the standard error is not best practise and should only be done if necessary.
#echo off
del /s *.jpg 1>nul
In this example, 1>nul only hides the standard output, but standard error will still show. If del fails to delete some files, you will be informed.
Read more about redirection

Try putting this at the top of your batch script:
#echo off

Related

Batch File Rd Error: The directory is not empty

I've been working on a code to delete the files from a Location inputted in .txt file.
#echo off
cd C:\Users\Troy\Desktop\Details
set /p loc=<Location.txt
rd /s /q "%loc%"
echo %loc%
pause
This code returns me the following output
The directory is not empty.
C:\Users\Troy\Downloads\TV Shows
Press any key to continue ...
Now the file Location.txt, when opened contains following
C:\Users\Troy\Downloads\TV Shows which is in accordance with the echo output I get in the second line (of the above output)
Also note that I have saved the batch file at C:\Users\Troy\Desktop
So there arises no reason for any interference due to the same location.
The weird part is when I run the following code from another batch file at the same location it runs perfectly fine and deletes all the files.
#echo off
set loc=C:\Users\Troy\Downloads\TV Shows
rd /s /q "%loc%"
echo %loc%
pause
So the only difference between the two codes is that the first one sets the variable location from a specific file, whereas the other one has a pre- inputted variable.
Also I have tried to delete files from the location using the following code
#echo off
cd C:\Users\Troy\Desktop\Details
set /p loc=<Location.txt
cd %loc%
del /s /q * >nul 2>&1
cd C:\Users\Troy\Desktop\Details
rd /s /q "%loc%"
echo %loc%
pause
In the above code, the delete command works perfectly fine and deletes all the files within. However folders and subfolders are all that are left, which means that rd command is not working
I've even tried the attrib -h thing, but that does not work either.
Also note that I've tried this with various permutations and combinations of rmdir /s /q too. But does not work.
Any help appreciated.
You could be suffering from some corruption in the file system. Try running chkdsk /f. You'll have to reboot in order to run it, but see if it finds something that it can correct, then see if your problem goes away.

Assistance modifying a batch file to delete the C: drive

Newbie question.
Would cd "C:del *.* delete all files within the C: drive?
I was also told if I add del . /F /Q it would bypass confirmation of the deletion, but I'm not 100% sure where to put it. Do I just add it onto the end like
cd "C:del *.* del . /F /Q?
This is a dangerous command. Be careful.
No, you probably want del /F /Q ..
The first part, del, is the command. This is telling it to delete something.
/F /Q are the options, denoted by the leading slash.
/F means to force it, so for example if a file isn't writable it will still delete it if able. It also skips some other checks.
/Q tells the program to be 'quiet', so less output will be generated than normal
.(dot) is the thing to delete, which is the place you currently are. If you are currently in C:\ (ie if the terminal displays C:\> at the beginning of every line), then it will be that that it deletes.
Note that I have only shown what you should type (del /F /Q .) and not what the entire line should look like when you're done (C:\> del /F /Q .). However, if this is going in a batch file then you want the first form only.
This is a destructive command and is provided merely to answer the query - don't try it at home or prank friends with it.
This will delete all files that are not locked, or opened for exclusive access by a program.
del c:\* /s /f /q /a
It will not delete every file in a Windows system drive and will not remove directories.

CMD "MOVE /Y" asks for confirmation

I'm still working on getting this: CMD for unable to move files due to string butchering to work.
My batch script looks like this right now,
FOR /F "delims=" %%T IN ('dir G:\ /B /A:D') DO (
CD "G:\%%T"
FOR /R %%D IN (*) DO (MOVE /Y "%%D" "G:\%%T")
)
PAUSE
and does as I intended, however as soon as it finds a duplicate file, it asks what it's supposed to do (overwrite: yes/no/all) for EACH file. Ordering to replace ALL only replaces one file.
Image: http://imgur.com/aKLsKs1
Why does it do that, and how do I fix it?
EDIT:
Turns out to be a windows bug of sorts. ROBOCOPY or XCOPY both work and their quiet switches work.
As you said, Robocopy does what you need and it smart about doing it.
http://social.technet.microsoft.com/wiki/contents/articles/1073.robocopy-and-a-few-examples.aspx
since windows 2000, the behaviour of the command is to prompt for confirmation regardless of the /y switch, unless the command is triggered from a script.
you can override this by setting the environment variable COPYCMD to /Y before running your move command. eg: SET COPYCMD=/Y && move /Y a b
this behaviour is documented at: https://ss64.com/nt/move.html
Funny bc.
/y : Suppresses prompting to confirm you want to overwrite an existing destination file.
/-y : Causes prompting to confirm you want to overwrite an existing destination file.
Have u tried a little /y ?
Strange thing...
move /? says (translated, because my help is german):
you have to confirm overwriting by default, in spite you call it from a batch file.
I just tried this very simple batchfile:
move test.txt test
test.txt is moved into folder test and the existing file is overwritten without confirmation.
When I give this command at the prompt (not batchfile), it asks for overwriting (as designed)
So all you have to do is removing /Y (strange, but fact)
(using Win7 - if this matters)

directory is not empty error

I try in my batch file o delete folder(BR) with many files and subdirectories, I try the following:
if exist C:\BR (
rmdir "C:\BR" /S /q
)
but sometimes I get an error that a specific folder is not empty.these folder contains files of CSS.
what the problem??
rd /s /q DIRNAME
rmdir /s /q DIRNAME
The files that you can't delete are in use.
Close whatever program is holding them open, probably your browser, and try again.
Let me guess, your trying to delete your %TMP% folder.
EDIT: To answer zipi's question.
It will delete every file and folder that it can. So, if c:\tmp\dir2\dir3\open.txt is open, c:\tmp\emptyDir is an empty directory, and you do this:
c:\>dir c:\tmp /b /s
c:\tmp\a.txt
c:\tmp\dir2\b.txt
c:\tmp\dir2\dir3\open.txt
c:\>rd /q /s c:\tmp
c:\>dir /s /b c:\tmp
c:\tmp\dir2\dir3\open.txt
You will have deleted:
c:\tmp\a.txt
c:\tmp\dir2\b.txt
And removed:
c:\tmp\emptyDir
But still have the directories...
c:\tmp
c:\tmp\dir2
c:\tmp\dir2\dir3
...an the file:
c:\tmp\dir2\dir3\open.txt
If instead, a.txt was open, you'd only have:
c:\tmp\
and
c:\tmp\a.txt
On win7 I use a simple bat file to go around the problem:
call :rmdir "my_directory_01"
call :rmdir "my_directory_02"
pause
goto :EOF
:rmdir
if exist %1 rmdir /s /q %1
if exist %1 goto :rmdir
goto :EOF
I had a similar problem. Tried lots of different solutions, but ultimately only the following worked:
rmdir c:\<directory> /s /q
Previously using other methods in CMD I was getting the following:
The directory is not empty.
I had the same issue and the solution is very silly. Please use /Q first and the /S to resolve your issue. so command should be something like:
IF EXIST %build_folder% RD /Q /S %build_folder%
Please let me know if this solves your issue.
Regards
Anuj
To remove directory in command line, you have to remove all of files and subsolders it cointainst at the first place. The problem may occur if some of those items are read-only. /f will try to force delete them.
Try
if exists C:\BR (del "C:\BR" /f /s /q)
Here you have MS docs of the DEL command: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/del.mspx?mfr=true
This worked for me
you will need to go to any drive where the folder is. Then right click on drive > properties > Check Scan disk or scan drive and for windows8 scan and repair
then go back to your folder and delete it
Batch - Getting "The directory is not empty" on rmdir command
In my case the failure to remove a directory using rd /Q /S and getting Directory not empty was down to file permissions. The batch job was doing a backup and removing oldest backup folder at the end to keep latest 10 backups. The normal user account only had read and execute permission on certain files in the sub folders. Running the batch file containing the rd commands under Task Scheduler with a tick in the option "run with highest privileges" enabled directory's to be removed.
You could achieve something similar as a one off if you run your batch file under cmd and choose run as administrator. In Windows 7 type CMD in the Search programs and files box, then right click on cmd.exe at the top of the popup window and click Run as Administrator. Then find and run your batch file in the cmd "black background" window.

Deleting file from batch file giving error

I am facing error when i try to delete a file using a batch file.
For example say the file i want to delete is "C:\test\a.dll"
i get the folder "c:\test" from registry and then i try to append the file name using and delete it using the following command
del /s %WPINSTDIR%\a.dll
where i get WPINSTDIR from registry and it would be "C:\test"
however when i try to run the batch file i get a error saying network path found
and this is the command that is executed.
del /s "c:\test"\a.dll
By setting a environment path variable i found that the problem is with the 2 slashes in "c:\test" and the quotes. Anyway to get around this problem.
Thanks
Try using
pushd %WPINSTDIR%
del /s a.dll
popd
This restores the former directory.
You can remove quotes around your environment variable with the following:
%WPINSTDIR:"=%
So the following might work:
del %WPINSTDIR:"=%\a.dll
It will fail, though, if the path contains spaces.
You can also use the following:
call :del_file %WPINSTDIR% a.dll
goto :eof
:del_file
del "%~1\%~2"
goto :eof
which should work even with paths containing spaces. The ~ in %~1 removes surrounding quotes.
This might do:
set current=%CD%
CD /d %WPINSTDIR%
DEL /s a.dll
CD /d %current%
EDIT
Edited to use CD /d and the "%CD%-trick".

Resources