I want to delete all files and folders in the directory, without deleting the root directory.
For eg: My tree structure is C:\app\temp.
The temp folder has a dist_timestamp folder and the dist folder has few files and folders. I would like to delete starting from dist folder, however if I am going to delete a dist_timestamp folder tomorrow, I cannot regenerate the timestamp the folder was created on. So technically, I would like to remove all folders under temp, without deleting temp.
I used rmdir /q C:\app\temp, and this seems to remove the temp folder also. Please help.
You can use this,
REM Opening the folder.
CD C:\app\
REM Deleting temp folder with all subdirectories and files.
RMDIR /S /Q temp
REM Creating new temp folder.
MKDIR temp
/S - Force deleting of read-only files.
/Q - Quite mode, do not ask if it ok to remove a directory tree with /S
This works:
pushd "C:\app\temp" && (rd /s /q . 2>nul&popd)
Related
I have a batch which copies some files from a directory and then should delete the directory the batch file is in, but it just deletes the contents and not the folder itself. I tried with rmdir and with del. The folder structure is the following:
F:\Copy\
copy.cmd
files\
something.jar
something2.jar
Copy.cmd should copy the .jar files, to the documents folder of the user and then delete the "Copy" folder, so it leaves no traces and does not occupy disk space, but my solutions always left the empty "Copy" folder.
My three tries
#echo off
title Copy
color 20
Xcopy "%cd%\files" "%userprofile%\Documents"
rmdir %cd%
and
#echo off
title Copy
color 20
Xcopy "%cd%\files" "%userprofile%\Documents"
del %cd%
and like Compo suggested
You cannot remove the directory if it's your current working directory. Just step out of it, then delete it using its absolute or relative path.
#echo off
title Copy
color 20
Xcopy "%cd%\files" "%userprofile%\Documents"
cd ..
rmdir %~dp0 /S
did not work.
I need to delete 4 files, and 1 folder in a specific location and in unknown number of subfolders(different names). I would like to keep this as simple as possible. I managed to make this almost work, without any loops, but it doesn't work with folder removal in subfolders. All 4 files are deleted in main folder and all subfolders, but folder "scss" with all his content is still present in subfolders.
How to force this script to additionally delete folder in subfolders too ??
START /c cmd %this_dir%
DEL gulp_start.bat, .brackets.json, package.json, gulpfile.js /s /q scss
rmdir scss
You will have to use a For Loop.
Example
Add following to your batch.
FOR /d /r . %%d IN (scss) DO #IF EXIST "%%d" rd /s /q "%%d"
Does somebody know a way to move a folder from one path to another with the "parent" folder ?
For example, all these folders:
Contain a folder named "Win" see below:
i want to copy the folder "Win" with the parent folder ( the numbers ) and move it to a new location example -> "C:\Storage\migration"
there are over 200 folders, so right now i am manually creating a folder named these numbers and copy & pasting the Win folder into it, it is very time consuming and not a very good long term solution :(
Maybe something could be done with a BAT. script or like ?
for /d %%a in ("d:\BaswareRay\OES\*") do (
xcopy "%%~fa\win\*" "c:\storage\migration\%%~nxa\win\" /y /s /e
echo rmdir /s /q "%%~fa\win"
)
For each folder under the indicated source, recursively copy its contents to the target folder and remove the source folder.
For each folder, the for command replaceable parameter %%a will hold a reference to the folder being processed. Using this reference, %%~fa will return the full path of the folder and %%~nxa the name and extension of the folder.
The deletion of source folder is only echoed to console. If the output is correct, remove the echo that precedes the rmdir command
I want to delete a folder with its contents recursively from a specific path under a cmd file. I don't want to delete other folders in the same folder as the path. I don't know in advance which folder has the folder I want to delete.
I did it with files but not with folders. Here i paste my code:
IF exist "\\PC1\temp\" (
rd "\\PC1\temp\"%dl% /s /q
ECHO Folder deleted successfully in PC1
) ELSE (echo PC1 is not connected)
NOTE: "%dl is a variable with the name of the folder to be deleted typed before from the user.
But if the folder exists inside another folder (for example: \PC1\temp\test) the folder canĀ“t be deleted even if I use the /s property.
Solution
Here is a DOS/CMD one-liner that will delete only the folder in the current directory that contains the subfolder "hello" in it:
for /d %d in (*) do if exist "%d\hello\." echo y | rd /s /q "%d"
NOTE: If you run this from a batch file you must change %d to %%d in all places.
Background on rd
You use the "/s" switch for rd command, combined with feeding a "y" to the command if you don't want to be prompted. Be warned that this command is very dangerous (think rm -rf in unix):
echo y | rd /s /q "\\PC1\temp\%dl%"
You don't have to check if the folder exists - just run it either way - it'll fail silently if it doesn't exist and succeed silently if it does.
There is a shared folder in my D drive as works (D:\works). I need to delete all the files in that folder and sub folders except word and excel files in there. how can i do this ?
You could do something similar to what this guy's done: http://www.codesingh.com/2009/08/using-robocopy-to-delete-old-files-from.html
Something like this should work:
mkdir D:\_tempDelete
robocopy D:\works D:\_tempDelete /e /MOVE /XF *.xls* *.doc*
rmdir D:\_tempDelete /s /q
Provided you have permissions to create and delete folders on D:. Otherwise you could just move the files somewhere on your local drive and delete them from there.