I have a folder C:\Epson Scans, I am trying to figure out how to write a script that will delete the contents of the folder but leave the folder intact. I have figured out how to delete the entire folder and I could recreate it. But I wanted to know if anyone knows a way of just deleting the contents inside the folder and not actually deleting the folder. Any help with this would be greatly appreciated!
Edit: Inserting working code so I can loop through many computers and do it at once. Will someone please tell me why the code is not working where I have inserted it?
#echo off
setlocal enabledelayedexpansion
set Delete_success=0
set total=0
for /F %%G in (pclist.txt) do (
set /a total+=1
pushd "C:\Epson Scans" || exit /B 1
for /D %%I in ("*") do (
rd /S /Q "%%~I"
)
del /Q "*"
popd
if !ERRORLEVEL!==0 (
set /a Delete_success+=1
) else (
echo EpsonDelete copy failed on %%G>>EpsonDelete_FailedPCs.txt
)
)
echo Delete Success: %Delete_success%/%total% >>EpsonDelete_FileCopy.txt
del deletes files only, so del /S /Q "C:\Epson Scans" deletes all files in the given folder and sub-folders (due to /S).
rmdir deletes folders, so specifying rmdir /S /Q "C:\Epson Scans" also deletes the folder Epson Scans itself.
Of course you could execute mkdir "C:\Epson Scans" afterwards to newly create the deleted folder again1, but this was not asked for. So the correct answer is to use a for /D loop over C:\Epson Scans and delete each folder it contains, and then use del /Q to delete the files:
pushd "C:\Epson Scans" || exit /B 1
for /D %%I in ("*") do (
rd /S /Q "%%~I"
)
del /Q "*"
popd
Note that rd is the same as rmdir -- see also this post: What is the difference between MD and MKDIR batch command?
1) Regard that some folder attributes get lost if you do that, for example the owner. Also the case is lost as Windows treats paths case-insensitively.
del /S C:\Epson Scans*
(use S to delete all files and folders in selected folder)
del C:\Epson Scans*.*
if this is batch file you might want to add /Q in order to avoid a delete confirmation dialog:
del C:\Epson Scans\*.* /Q
Related
I am trying to delete everything in a user defined location with exception on one pre-defined folder using a for loop. How do I go about adding a exception in order to not delete a folder.
I am trying to learn how to code, but I admit I am doing baby steps. I got some excellent tips for the first input part of this script, but I lack the knowledge to move forward. I have searched and found similar code, but none seems to work. This script is intended for flight simulation and hopefully ease the workload of installing a particular item.
This is just the part of the code due to stackoverflow guidelines, it deletes everything including the folder I want to exclude.
...
Rem This code is intended to delete all except one pre-defined folder
Echo Deleting all the files except testmappe3
del /s /q "%CD%"
for /d %%p in ("%CD%") do rmdir "%%p" except "%%testmappe3" /s /q
dir
Pause
...
I expected the output to delete all folders except testfolder3
for /d %%A in ("%CD%\*") do (
set "except="
if /i "%%~nxA" == "testmappe3" set "except=1"
if not defined except rmdir /s /q "%%~A"
)
This code will iterate the folders in the current directory.
If the name+extension of the folder is testmappe3,
then except will be set as 1 i.e. defined with a value.
If except is not defined, rmdir will remove the folder.
You can add more if lines for checking folders to except.
The modifiers will recognize a folder such as named
testmappe3.test1 as name testmappe3 and
extension of .test1.
View for /? and call /? about modifiers.
View for /?, set /?, if /? and rmdir /? for
help with those commands.
First of all, I would be a very careful deleting everything using %cd% especially if the script can accidently be run as Administrator, where %cd% would then be c:\windows\system32.
Instead, use %~dp0 as path to ensure that you are in the correct directory. This all assumes you did not cd somewhere else earlier in the script.
Then to the actual issue, I would include findstr to exclude your directory `testmappe3 as well as your script itself.
#echo off
cd /d "%~dp0"
for /f %%p in ('dir /b ^| findstr /vi /r /c:^testmappe3$') do (
rmdir "%%p" /s /q >nul 2>&1
if not "%%p"=="%~nx0" del /s /q "%%p" >nul 2>&1
)
If you want to stick to your original delete method, then it would be as below, but if your script is in the same dir, then it will also be deleted:
#echo off
cd /d "%~dp0"
del /s /q *
for /f %%p in ('dir /b ^| findstr /vi /r /c:^testmappe3$') do (
rmdir "%%p" /s /q >nul 2>&1
)
If your folder to exclude contains spaces, double quotes are required.. i.e
dir /b ^| findstr /vi /r /c:^"test mappe3"$
For this type of directory structure:
\\rdwlhsdevserver\root\user1\folders\testdat.txt
\\rdwlhsdevserver\root\abhay\testdat.txt
\\rdwlhsdevserver\root\testuser\folders1\folder2\testdat.txt
\\rdwlhsdevserver\root\devadmin\input\testdat.txt
\\rdwlhsdevserver\root\admin\testdata\testdat.txt
I know that I can use del /s /q \\rdwlhsdevserver\root\* to delete files from parent folder and all sub-folders. But I want to delete all folders and files except \\rdwlhsdevserver\root\<folder>\.
After running cmd output should be like:
\\rdwlhsdevserver\root\user1\
\\rdwlhsdevserver\root\abhay\
\\rdwlhsdevserver\root\testuser\
\\rdwlhsdevserver\root\devadmin\
\\rdwlhsdevserver\root\admin\
pushd "\\rdwlhsdevserver\root" && (
for /d %%a in (*) do ( cd "%%a" && ( 2>nul rmdir . /s /q & cd .. ) )
del /f /q *
popd
)
This will change the current active directory (pushd) to the target directory and if there are no problems (conditional execution operator &&) for each folder (for /d) change to it (cd), remove its contents (rmdir) and return to the parent folder. Once done, remove (del) the files inside the root folder and restore the initial active directory.
Why not change the inner for to for /d %%a in (*) do rmdir "%%a" /s /q ? Because this will also remove the folder. But if we first make the folder the current active directory (cd) we will be able to delete its contents but not the folder itself as it is in use (the 2>nul is a redirection of the stderr stream to nul to hide the error in the rmdir saying it can not remove the folder because it is in use)
You need to iterate over all sub-directories of \\rdwlhsdevserver\root\ using a for /D loop, then loop over each sub-directory's sub-directories again by another for /D loop, then apply the rmdir (or rd) command on each of the returned items, like this:
for /D %%J in ("\\rdwlhsdevserver\root\*") do (
for /D %%I in ("%%~J\*") do (
rmdir /S /Q "%%~I"
)
)
Or in command prompt directly:
for /D %J in ("\\rdwlhsdevserver\root\*") do #for /D %I in ("%~J\*") do #rmdir /S /Q "%~I"
For the sake of overall performance, if you want to delete directories and files, I recommend to do the above directory removal before the file deletion (del /S /Q "\\rdwlhsdevserver\root\*"). Note that your del command line also deletes files located in the directory \\rdwlhsdevserver\root\.
I use batch file commands to delete the temp files in the system. The command works OK.
This code, works normally, but there is a flaw:
FOR /D %%p IN ("C:\Temp\*.*") DO rmdir "%%p" /s /q
cd c:\temp
del /F /s /q *.* >c:\DelTempLog.txt
rd /s /q %systemdrive%\$Recycle.bin >c:\DelTempLog.txt
FOR /D %%p IN ("C:\Windows\Installer\$PatchCache$\*.*") DO rmdir "%%p" /s /q
cd C:\Windows\Installer\$PatchCache$
del /F /s /q *.* >c:\DelTempLog.txt
FOR /D %%p IN ("C:\Windows\Temp*.*") DO rmdir "%%p" /s /q
cd C:\Windows\Temp
del /F /s /q *.* >c:\DelTempLog.txt
del /q /s %tmp% >c:\DelTempLog.txt
Today I faced an exception where c:\temp folder did not exist on the server.
It deleted half of the files under c:\windows\system32.
I want to add an IF command after changing the DIR before deleting anything.
Also, please advise me how to do logging activity in a better way.
At an elementary level if you specify the full path on the command line then it cannot delete files from anywhere else.
del /F /s /q "c:\temp\*.*?"
There is also no need to change the directory before issuing the command.
The ? suppresses a prompt that asks if you are sure that you want to delete all files.
How about, before your batch-as-it-stands, you try
md c:\temp 2>nul
if not exist c:\temp\. echo No c:\temp!&goto :eof
#echo off
setlocal enableextensions disabledelayedexpansion
set "logFile=c:\deltemplog.txt"
(for %%a in (
"c:\temp" "c:\windows\temp" "%temp%"
"%systemdrive%\$Recycle.bin" "C:\Windows\Installer\$PatchCache$"
) do if not exist "%%~a\" (
echo [ ERROR ]: "%%~a" does not exist
) else pushd "%%~a" && (
echo [ pushd ]: changed to "%%~a"
echo rmdir . /s /q
popd
) || (
echo [ ERROR ]: Failed to change to "%%~a"
)
) > "%logFile%"
For each folder in the list, change to it and if the command did not fail, remove the current folder (this will remove the content, not the folder, as it is the current one).
The rmdir commands are only echoed. If the output (in the log file) is correct, remove the echo command that prefixes rmdir
My preference is to use the %CD% built-in value, then try to move to the folder and see if it worked, as in:
set CURDIR=%CD%
pushd C:\Temp
if '%CD%'=='%CURDIR%' (
echo Failed to move to C:\Temp
) else (
[code to do your deletions]
)
popd
I prefer to push and pop a directory rather than a hard CD to it, simply because then you can always get back to where you were without having to know where that was.
What do I add to my DEL batch file if I want to exclude a folder inside a folder I want to delete?
I have this code to delete my all the contents of the temp folder
DEL /F /Q C:\temp
Now, I want to exclude a folder named imptfolder inside. This shouldn't be deleted whether it exists or not inside the temp folder. How do I do this?
Here's some batch script code that'll do what you're asking.
for /d %%I in (c:\temp\*) do (
if /i not "%%~nxI" equ "imptfolder" rmdir /q /s "%%~I"
)
del /q c:\temp\*
If you're just entering these commands from the console, use single percents rather than double.
cd /d c:\temp
for /d %I in (*) do #(if /i not "%~I" equ "imptfolder" rmdir /q /s "%~I")
del /q *
I'm trying to make a program that will delete some files and perfrom rutine maintance on a computer by just clickin on one file. I'm testing it as I'm going along and realized that it's not deleting the folders. I want to delete everything within the folders but not the folders themselves. Here is my code so far:
#echo off
title SYSTEM Optimiation
echo Deleting Temp Folder
del /q /f "C:\Documents and Settings\%username%\Local Settings\TEMP"
echo.
echo DONE
echo.
echo Deleting Download folder
del /q /f "C:\Documents and Settings\%username%\My Documents\Downloads"
echo.
echo DONE
echo.
echo.
echo Hit any key to exit.
pause >nul
Try using wildcards and the /s switch on del:
del /q /s /f "%userprofile%\My Documents\Downloads\*"
but this will probably leave directories inside intact, but empty. Another option would be the quite explicit:
for /d /r "%userprofile%\My Documents\Downloads" %%x in (*) do rd /s /q "%%x"
for /r "%userprofile%\My Documents\Downloads" %%x in (*) do del /f "%%x"
Here much simpler than above. Current directory will be locked and therefore will not be deleted with others.
cd %Temp% && rmdir /s /q .