dos command to delete a folder except the named sub folders - dos

Assume I have a directory in your computer under C:/dir1
And inside "dir1" we have more directories dir11, dir12, dir13,
And in each of the above directories we have more like below
dir11- dir111, dir112 dir113
dir1-dir121, dir122, dir123
dir13 - dir132,dir132,dir133
Now I need to find a command or a small script, which can delete everything under dir1 except couple of directories say dir122 and dir132.
Can you find something using DOS commands ?

You can use dir filespec /b >tmp.bat to list the file names in a text file called tmp.bat. Then edit that file to add del before each file name you want to delete. You can remove the file names you want to keep from the batch file and then do a "change all" to add del to each line. When it is edited properly, execute the batch file.
Alternatively, you could write a quick program under vb.net to do it.

Using the given examples: this should echo all the rd commands for the other folders.
#echo off
for /f "delims=" %%a in (' dir "c:\dir1" /b /s /ad ^| findstr /v /i "dir122 dir132" ') do echo rd /s /q "%%a"

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"

shell script to rename a file name based on another file name

I have 2 files in my folder. One is "TEST ONE.txt", the other is "LEARN NEW.vcd".
I need to create a batch script to rename the *.txt using the *.vcd.
So the Batch script should rename the "TEST ONE.txt file" to "LEARN NEW.txt".
I have only one file with txt and one file with vcd extension in my folder C:\DEV folder.
I tried using the following script but it does not pick up the full name of the vcd extension and drops everything after the spaces - so I just get LEARN.txt :(
#echo ON
CD "C:\DEV folder"
setlocal enabledelayedexpansion
for /F %%A in ('dir /b *.vcd') do (
set xname= %%~nA
ren *.txt !xname!.txt
)
Thanks - db
It appears that the default in FOR /F may be to only take one (1) token. You can force it to take all using tokens=*. Quoting is important.
#echo ON
CD "sub"
setlocal enabledelayedexpansion
for /F "usebackq tokens=*" %%A in (`dir /b *.vcd`) do (
set xname=%%~nA
ren "*.txt" "!xname!.txt"
)
Just in case someone stumbles across this the way I did, there is a more elegant solution that I was forced to devise because I wanted it to go through multiple sub-directories, rather than run it one directory at a time. It is:
forfiles /S /M *.vcd /C "cmd /c rename *.txt #fname.txt"
Use your file extensions of choice.

I need a batch file to generate a list with *only* file names

I need it to work on Win10 and Win7 machines. If I can get this to work I'll make a batch file.
Winkey, "cmd"
cd "e:\media\trainingvids"
dir *.* /s /b /a -d > c:\temp\dork.txt
So, to state the obvious but make sure I'm getting it, I'm opening a command prompt, changing to the correct directory, doing a directory listing of all files (including sub-directories (/s), no headers or footers so 'bare' format (/b), and trying to NOT display the directory (/a -d) – and then sending/piping that (>) to a file I've designated to be named and created (dork.txt) in a temporary directory (\temp) that already exists on my c:.
The problem is that it doesn't work. I'm not able to find a way to NOT include the full path along with the file names. I need a nudge with the syntax. Or maybe I've got it all wrong and it can't be done in this way.
What does my Basic batch file look like that can do this?
You will need the for /F command to accomplish this:
> "D:\temp\dork.txt" (
for /F "eol=| delims=" %%F in ('
dir /B /S /A:-D "E:\media\trainingvids\*.*"
') do #(
echo(%%~nxF
)
)
You placed a SPACE between /A and -D in your dir command line, which must be removed.
Since I stated the full path to the target directory in the dir command and also to the output file, you can save this script at any location and run it from anywhere.
for /f "delims=" %%a in ('dir /s/b/a-d') do echo %%~nxa
should accomplish that task.
If you can tolerate double quoted names this batch file works well.
set myPath=c:\temp
set myMask=*.pdf
set myLog=c:\temp\myLogFile.log
FORFILES /P %myPath% /S /M %myMask% /C "CMD /C ECHO #file >> %myLog%"
Alter the values to meet your needs.
You're almost there with:
dir /s /w /a-d | find "."
The only drawback is that you get the file names in columns, and possibly a Volume line which you can remove with another find filter.

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."

cmd: Find updated folder and delete it using cmd

Using CMD line, in a given directory, I want to detect the most recently created/written folder and delete all the contents of that folder.
Any help/suggestions would be helpful.
This command prints all subdirectories in order of their last write/created time in reverse order (latest directories first):
DIR /A:D /O:-D /TW /B
To delete a directories' contents, a simple
DEL /S /Q "directory"
should be sufficient
If you want to process only the first result of the DIR command, you can use a FOR loop in a batch file, that leaves after the first iteration.
It should look something like this:
#ECHO OFF
REM delete all contents from the sub directory most recently created or written to
FOR /F "delims=" %%A IN ('DIR /A:D /O:-D /TW /B') DO (
RD /S /Q %%A
EXIT /B
)
Only works for the subdirectories of the current working directory, so use with care!
I guess for empty directories there will be some weird output, but I didn't test it.
EDIT:
Updated the batch file to remove the whole directory and its content using:
RD /S /Q "directory"

Resources