How to delete an intermediate directory in windows using cmd? - windows

For example to go from this:
directory1/
101/online/img1.png
102/online/img2.png
103/online/img3.png
To this:
directory1/
101/img1.png
102/img2.png
103/img3.png
In this example, 101 (and 102, 103...) directories contain only a single directory online, which is always of the same name. However, online directory may contain multiple files inside, but no directories.
I am looking for an automated way to manipulate my files like that, as I have a big list of such cases. I am looking for a Windows Command Prompt or Powershell solution.

New answer and updated
This from the command line:
pushd directory1
for /D %D in (*) do move /Y %D\online\*.* %D && rmdir /s /q %D\online
popd
Or this from a cmd file:
pushd directory1
for /D %%D in (*) do move /Y %%D\online\*.* %%D && rmdir /s /q %%D\online
popd

Related

deleting folders recursively using windows batch script

I have a folder with multiple subfolders under it. I would like to delete all test folders under the subfolders/example. How can I do this using windows batch script? Note that there is no test folder in some.
I know how to delete mainfolder/subfolder1/example/test. But am stuck with recursively deleting under each subfolder i.e delete mainfolder/*/example/test.
TIA
E.g:
mainfolder
subfolder1
source
example
test
subfolder2
source
example
test
subfolderX
source
example
for /r "c:\sourcedir" /d %a in (*) do if /i "%~nxa"=="test" echo rd /s /q "%a"
direct from the prompt - double each % to use as a batch line.
replace c:\sourcedir as appropriate
required rd is merely echoed to show what he script intends to do. Remove the echo keyword after testing to actually perform the deletion.
I recommend looking at ss64.com's CMD.EXE reference, specifically the DIR, FINDSTR, and FOR commands.
for /f %i in ('dir /s /a:d /b ^| findstr /i /e "example\test"') do rmdir /s %i
appears to be the appropriate command for what you have requested.
Notes on DIR: /S - Subdirectories, /A:D - directories only ("Attribute:Directory"), /B - "bare", no headers or footers, just the full pathname.
Notes on FINDSTR: /I - Case-insensitive, /E - Match at end-of-string
Verbose descriptive summary of command: Create a list of directories, including all subdirectories, and select only those that end in "example\test", then remove each of them, including all files and subdirectories in them.
for /R "mainfolder" /D %%a in (example\te?t) do rd /S "%a"
The only inconvenient of this method is that the name of the target folder must be given as a wild-card, so you must give a name that does not include any other undesired folder. If the name is given with no wild-card, the for command may include other folders.
You just need to iterate through the main directories, and check for the existence of the directory that you want to delete:
for /D %%d in (mainfolder\*) do if exist "%%d\example\test" rd /s /q "%%d\example\test"

Windows: Deleting folder common in multiple sub-folders

Is there a way to do this? I have a folder structure as such:
A/
Folder/
B/
Folder/
C/
Folder/
D/
Folder/
And I want to delete all of the "Folder/"s, along with all of their contents. My first guess was
rmdir /S /Q *\Folder
but received an error on the *. This has come up a few times in the past days, so I figured I'd get the more efficient way than going into each folder, because that wasn't fun.
Have you try this ?
for /D %i in ("C:\Directory\*") do RD /S /Q "%i"
Put double % if you want to do this in batch file
This should display the rd command for every directory that matches the folde? filespec under the current tree.
The wildcard is necessary so check the output before removing the echo
#echo off
for /d /r %%a in (folde?) do echo rd /s /q "%%a"
pause

Windows 7 empty entire directory from command line

I am running a batch file which deploys a war file to a specific directory. However, I want to empty that directory before deploying to it, recursively.
I found this script that is supposed to remove everything in the directory including folders and files but I get this error: "%%i was unexpected at this time."
FOR /D %%i IN ("C:/Program Files (x86)/Apache Software Foundation/Tomcat 6.0/abtapps/ROOT/*") DO RD /S /Q "%%i" DEL /Q "C:/Program Files (x86)/Apache Software Foundation/Tomcat 6.0/abtapps/ROOT/*.*"
I just need a simple way to recursively empty my ROOT directory either by fixing the script above or with another method. Have to be able to do this from a batch file.
I am running windows 7.
SOLUTION
rmdir ROOT /s /q
mkdir ROOT
You should use rmdir /S. Check out this link
I think what you are looking for is to clear the folder not to delete it and recreate it. This will actualy clear the folder so you don't have to recreate it.
CD "C:/Program Files (x86)/Apache Software Foundation/Tomcat 6.0/abtapps/ROOT" && FOR /D %i IN ("*") DO RD /S /Q "%i" && DEL /Q *.*
A few notes.
You only use a single % when running from the command line. doubles are for batch files.
&& is for running multiple commands on the same line.
* The &&'s are especially important in this case because you do not want the del /q . to run if the cd fails as you will delete to contents of what ever directory you started in.
Try this:
forfiles /p "folder" /s /c "cmd /c rd /q #path
replace "folder" with your folder name or drive letter
example: forfiles /p f: /s /c "cmd /c rd /q #path
This will recurse through all subfolders, and only remove the empty folders.
If you use it in command line, you'll get error messages for folders that aren't empty. If you put it in a batch script, it will take some time to run, depending on how many folders you have.

Delete contents of a directory recursively on Windows

I need to delete the entire contents of a directory (nested folders and all) without deleting the directory itself. Recreating the directory after the fact is not an option as it is being locked by the running process and delete of it would fail.
So far I have the following:
rd /s /q dir1
rd /s /q dir2
rd /s /q dir3
del /q /f *
It works, but the obvious problem is that I have to update this script every time the set of first-level directories changes.
On UNIX, I would solve this like this:
rm -rf *
What is the Windows equivalent?
Assuming that you are executing the command from the top-level directory:
for /d %X in (*.*) do rd /s /q %X
If you are executing this from a script, you must use double percent signs:
for /d %%X in (*.*) do rd /s /q %%X
If you need to delete the files in the top-level directory as well, add this to the script:
del /q /f *
I know this is an old question with an old answer, but I've found a simpler way to do this and thought of sharing it.
You can step into the target directory and use the rd command. Since Windows will not allow you to delete any files or directories currently in use, and you are making use of the target directory by stepping into it, you'll delete all the contents, except the target directory itself.
cd mydir
rd /s /q .
You'll get a message saying:
The process cannot access the file because it is being used by another process.
This will occur when, after deleting all the contents, the rd command fails to delete the current directory, because you're standing in it. But you'll see this is not an actual error if you echo the last exit code, which will be 0.
echo %errorlevel%
0
It's what I'm using and it works fine. I hope this helps.

How to delete files recursively

I have the directory structure /foo/bar/fooBar/.. . I want to write a Windows command where I can mention the path till foo directory and it deletes all the files and directory recursively in /foo, but it should NOT delete the foo directory.
I have been using rmdir /q /s [path to foo] but this command deletes the foo directory as well. Let me know if there is any command(s) to accomplish this.
rd /s /q /path/to/foo
md /path/to/foo
del /f /s /q DirectoryWhichContainsFilesToDelete/\*
This will delete all files in the folder DirectoryWhichContainsFilesToDelete without deleting the folder itself.
Have fun :)
I had been scratching my head on this one as well. It is easy enough to create a for loop that uses rmdir however it leaves behind folders that have spaces in the long names. It is possible to manipulate a dir list and get the 8.3 filenames however here is a much simpler solution.
Create an empty folder then;
robocopy \empty_folder \folder_with_sub_folders /PURGE
All subfolders & files will be deleted.
del X /f /s /q
rd X /s /q
this WILL remove the ROOt directory though. make it again with
md X
or make a copy of it first.
otherwise you'll have to do batch funkiness
dir X /ad /b
will give you a list of the immediate subdirectories of X. you can work out the rest
I was looking for a simple command to delete all files in a directory recursively but leaving the directory structure unchanged. So, maybe this could be interesting ;)
for /f "delims=" %i in ('dir /B /S /A:-DH') do #del /F /Q /A:H "%i"
The command 'dir /B /S /A:-D' lists only files (/A:-D) in current directory recursively (/S) without 'dir' summary report (/B). The 'for' loops through each full line (/delims=) and executes the delete command, forced and quiet. I additionally used the hidden flag (/H) both for listing and deletion for some mysterious (e.g. thumbs.db) files.
deltree /foo/* should work fine.
I have used this in a batch file in the past. It uses a for loop to navigate the directory structure.
Here I remove the cvs sub directories off of a tree, needed when copying from one branch to another.
#echo off
if /I exist CVS. rd CVS /s /q >nul
for /F %%z in ('dir cvs /ad /s /b') do echo %%z && rd /s /q %%z
echo Batchfile %0 is complete
Try to use Powershell:
powershell -Command "Remove-Item '\foo\*' -Recurse -Force"
To prevent deleting of the foo directory try change directory to foo prior to the delete such as:
cd c:\foo
rd /s /q c:\foo
This will delete all the files and folders under foo but NOT foo. An error message will be displayed as follow "The process cannot access the file because it is being used by another process."

Resources