I'm trying to clear a folder of all it's contents but a certain sub-folder and its contents. My deleting works fine but I can't figure out how to exclude.
cd C:\testfolder
del * /S /Q
rmdire /S /Q "C:\testfolder"
But I do not want to delete the folder C:\testfolder\subf. How can I do this?
If you are using at least windows vista (robocopy command is used), this should do the job
#echo off
setlocal enableextensions disabledelayedexpansion
rem Create a temporary empty folder
set "tempFolder=%temp%\%~nx0.%random%%random%%random%.tmp"
md "%tempFolder%" >nul 2>nul
rem Purge from target folder anything not in the empty source folder,
rem but exclude the indicated folder
robocopy "%tempFolder%" "c:\testfolder" /nocopy /purge /xd "c:\testfolder\subfolder"
rem Cleanup
rmdir "%tempFolder%" /s /q >nul 2>nul
If you mean you want to delete all files but not the subfolder you can just use the following
cd C:\testfolder
del *.* /S /Q
This will delete any files in the "testfolder" and any files in the subfolders but will leave the subfolder there.
Related
I am trying to create a simple batch file to delete a folder and/or its contents for a user when they logoff or shutdown.
The GPO itself is working and running, I know this because it first creates a .txt document before running the delete commands.
batch file is called "delete.bat"
The GPO first copies over the batch file to C:\delete.bat. the GPO does this every time all the time.
it is set to run this file on either shutdown or logoff. right now it only runs on shutdown.
i have several versions of this file
echo.>"C:\Users\myactualname\testyoyo.txt
del "C:\Users\%username%\cpsi\data_4\*.*" /s /f /q
#RD /S /Q "C:\Users\%username%\cpsi\data_4\"
exit
.
#echo off
sleep 4
echo.>"C:\Users\myactualname\testyoyo.txt"
sleep 2
set fld=C:\Users\myactualname\cpsi\data_4\
for /f %%a in ('dir %fld% /b /a-d') do echo del "%fld%%%a"
for /f %%a in ('dir %fld% /b /ad') do echo rmdir /s /q "%fld%%%a%"
.
#echo off
echo.>"C:\Users\myactualname\testyoyo.txt"
#echo off
rem Delete all files and subfolders in directory for temporary files
rem of current user account, but keep the directory itself. Temporary
rem files and subdirectories currently in use are silently ignored.
del /F /Q "C:\Users\%username%\cpsi\data_4\*" 2>nul
for /D %%D in ("C:\Users\%username%\cpsi\data_4\*") do rd /Q /S "%%~D" 2>nul
rem Do the same as above for system temporary files directory.
rem This cleanup requires administrator privileges.
del /F /Q "C:\Users\%username%\cpsi\data_4\*" 2>nul
for /D %%D in ("C:\Users\%username%\cpsi\data_4\*") do rd /Q /S "%%~D" 2>nul
i also ran some other version which i haven't documented. I've tried hardcoding the file paths for testing and using the %username% wildcard.
on shutdowns it will create the txt file but not on logoffs. and at no time will it delete the files/folders.
the bat files all run fine if manually run.
i am pretty sure the file permissions are good.
however i suspect permissions is what i suspect is the problem.
thanks!!!
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
I have made a bat script as follows
cd "D:\ACT\ACTBKUP"
del /Q *.*
FORFILES /P E:\ACT_Backups /M *.zip /D +1 /C "cmd /c del #D:\ACT\ACTBKUP"
The script is supposed to delete everything in "D:\ACT\ACTBKUP" and then move the newest zip folder to that same folder from E:\ACT_Backups. When I run this script from windows server 2012 it just disappears.
thanks
In order to switch to a directory that is located on a different drive, you need to use cd /d instead of just cd. If you do not do this, the directory will not change.
When you run a script by double-clicking on it, batch considers the current directory to be the directory where the script is currently located. Since you are not using the /d option with cd, you are running del /Q *.* on the directory where the script is located.
To fix this, you have two options:
cd /d "D:\ACT\ACTBKUP"
del /Q *.*
or
del /Q "D:\ACT\ACTBKUP"
There is no option in forfiles to get just the most recent file; /D +1 will return all files with a last-modified date of today or later. In order to get the most recent file and nothing else, you will need a for /f loop:
rem /b returns just the file name and /o:d sorts the files by date
rem Since newest_file gets set for each .zip file in the directory,
rem the last file set will be the newest
for /f "delims=" %%A in ('dir /b /o:d E:\ACT_Backups\*.zip') do set newest_file=%%A
copy %newest_file% D:\ACT\ACTBKUP
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 need a batch file that prompts the user for a file path, and the first 3 letters in the folder name. Then the batch goes and deletes all the folders and files in the folders with these 3 letters, but keeps the folder starting with the 3 letters. I got a batch file that deletes all files in folder and sub-folders, but I also need it to delete the sub-folders. Here was my code for deleting all files:
#echo off
set /p filepath=Path to Main Directory:
set /p string=First Letters of Sub-Folders to be Cleansed:
FOR /D %%G in ("%filepath%\%string%*") DO del "%%G\*.*" /q /s
echo Done
pause
Then I tried a lot of different ways to also delete sub folders of %%G. I got as far where it names the sub folders of %%G here:
#echo off
set /p filepath=Path to Main Directory:
set /p string=First Letters of Sub-Folders to be Cleansed:
FOR /D %%G in ("%filepath%\%string%*") DO for /r /d %%i in (%%G\*) do del "%%G\*.*" /q && echo %%i
echo Done
pause
But when I try to add && rd /s /q %%i at the end it says file and path not found.
No need to recursively look for folders in your "exp1" folder. You can simply use FOR /D to list the child folders and then use RD /Q /S to remove the entire folder hierarchy rooted on each child. Also, since all the child folders are being removed anyway, there is no need to do a recursive file delete.
#echo off
set /p filepath=Path to Main Directory:
set /p string=First Letters of Sub-Folders to be Cleansed:
for /d %%G in ("%filepath%\%string%*") do (
del "%%G\*" /q
for /d %%F in ("%%G\*") do rd /s /q "%%F"
)
echo Done
pause