Dos Batch File Commands Delete
Sep 17, 2012 Hi, I want to have a batch file that will: 1. Check to see if the file exists 2. Try to delete it if it does exist 3. Output what happened I got something. Del 'file name/ or *.txt etc.' Will delete the file in the current directory your batch is looking at, just don't add a directory path before the file name and just have the full file name or, to delete multiple files with the same extension with *.txt or whatever extension you need.
The DEL command Basic syntax DEL filespec filespec: files to be deleted Full syntax MS-DOS and PC-DOS (incl. Win95's MS-DOS 7) as specified under basic syntax as specified under basic syntax, plus some added features as specified under basic syntax, plus many new features To delete all files from a directory, you could use: DEL. DEL will then ask you to confirm that you want to delete all files. In batch files, this would stop the execution of the batch file and require user interaction, which usually isn't what batch files are intended for. One way to solve this would be: ECHO Y DEL. which will work fine for English DOS versions; for other languages the 'Y' needs to be replaced. So what if you don't know what language will be used for your batch file?
Try this: FOR%%A IN (.) DO DEL%%A This solution is completely independent of both operating system and language! Related Stuff.

MS-DOS' command deletes entire directory trees. The command in Windows NT 4 and later.
Dos Batch File Commands Copy
In Windows NT 4 and later, use RD /S /Q instead of DELTREE. sends the contents of a folder to the Recycle Bin instead of truly deleting them. In VBScript you can use my subroutine. and DelOld (one and ) delete files older than a specified number of days.
MOVEEX by schedules files that are in use, like system DLLs, to be removed at the next reboot. OS/2's command. Download or, two DELTREE replacements for OS/2.
If you want to delete all files in a folder, including all subfolders and not rely on some error conditions to keep the root folder intact (like I saw in another answer) you could have a batch file like this: @echo off REM Checking for command line parameter if '%1' ( echo Parameter required. Exit /b 1 ) else ( REM Change directory and keep track of the previous one pushd '%1' if errorlevel 1 ( REM The directory passed from command line is not valid, stop here. Exit /b%errorlevel% ) else ( REM First we delete all files, including the ones in the subdirs, without confirmation del.
/S /Q REM Then we delete all the empty subdirs that were left behind for /f%%D IN ('dir /b /s /a:d '%1') DO rmdir /S /Q '%%D' REM Change directory back to the previous one popd REM All good. Exit /b 0 ) ) And then you would simply call it with: emptymyfolder.bat 'C: whatever is my folder'. This worked better for me when I had spaces in the folder names. @echo off REM - Batch file to clean out a folder REM Checking for command line parameter if '%1' ( echo Parameter required. Exit /b 1 ) else ( echo. echo.
Deleting all files, including the ones in the subdirs, without confirmation. del '%1.' /S /Q echo. REM Deleting all the empty subdirs that were left behind FOR /R '%1'%%D IN (.) DO ( if '%%D'%1.' ( echo. Cleaning out folder:%1.
) else ( echo Removed folder '%%D' rmdir /S /Q '%%D' ) ) REM All good. To delete file: del PATHTOFILE To delete folder with all files in it: rmdir /s /q PATHTOFOLDER To delete all files from specific folder (not deleting folder itself) is a little bit complicated.
Del /s. cannot delete folders, but removes files from all subfolder. So two commands are needed: del /q PATHTOFOLDER.
for /d%i in (PATHTOFOLDER.) do @rmdir /s /q '%i' You can create a script to delete whatever you want (folder or file) like this mydel.bat: @echo off setlocal enableextensions if '%1' ( echo Usage:%0 path exit /b 1 ):: check whether it is folder or file set ISDIR=0 set ATTR=%a1 set DIRATTR=%ATTR:0,1% if /i '%DIRATTR%'d' set ISDIR=1:: Delete folder or file if%ISDIR%1 (rmdir /s /q '%1') else (del '%1') exit /b%ERRORLEVEL% Few example of usage: mydel.bat 'path to folder with spaces' mydel.bat path to fileorfolder.