Delete everything from Subfolders through Batch file - batch-file

I have directories like:
c:\Project\Current\stage1\somefiles and some folders
c:\Project\Current\stage2\somefiles and some folders
c:\Project\Current\stage3\somefiles and some folders
c:\Project\Current\stage4\somefiles and some folders
c:\Project\Current\stage5\somefiles and some folders
.
.
.
c:\Project\Current\stage500\somefiles and some folders
I want to create a batch file so that everything inside stage1, stage2,..., stage500 will get deleted but not any of other folders so that I can still see the above directories but empty.
Can someone please help?

Try this:
#echo off
CD c:\Project\Current /d
for /f "tokens=*" %%f in ('dir /a-d /s /b') do (
del "%%f" /q /f
)
There are three important parts:
for /f "tokens=*" %%f means we are iterating over all lines that are generated by the following command and temporarily save each line in the variable %%f for each iteration.
dir /a-d /s /b is the core of the code. This will list all files inside c:\Project\Current\ including all subfolders. /a-d means that directories will be ignored as we don't want them to be erased. /s means we are searching any subfolder. /b sets the output format to simple mode so that each line of the output will contain nothing but the full path to a file.
del "%%f" /q /f simply deletes the file which is stored in %%f. /q means "don't ask me if I'm sure, just erase it" and /f means that any file - even if it is marked as system file or as invisible or protected - will be deleted. Don't miss the quotation marks around %%f as otherwise paths containing spaces will cause trouble.

I found the answer and is very simple
for /d %%X in (c:\Project\Current*) Do (
for /D %%I in ("%%X\*") do rmdir /s/q "%%I"
del /F /q "%%X\*")
Thanks for everyone's help..

Related

xcopy batch issue

I want to copy a set of subfolders where name contains items on a list. The list has a set of codes (e.g. ABC1, ABC2) but the folders are named ABC1_revised_2018, etc. My batch file I put together is below. What I am getting a '"Usebackq tokens=^" was unexpected' error.
#ECHO ON
SET FileList=C:\filelist.txt
SET Source=C:\Files
SET Destination=C:\Files-Parsed
FOR /D "USEBACKQ TOKENS=^" %%D IN ("%FileList%") DO XCOPY /E /F /D "%Source%\%%~D" "%Destination%\"
GOTO :EOF
I am attempting to use ^ to denote match beginning of string but that clearly isn't working. Any ideas? I have tried with a batch file and also line by line in cmd.
append
Folder
-ABC1-text-date (this is a subfolder)
-ABC2-text-date
filelist.txt only has values like ABC1, ABC2, etc. not exact matches does this help?
Well, if you want to recurse through directories and copy sub directories as per partial matches inside the file:
#echo off
set "FileList=C:\filelist.txt"
set "Source=C:\Files"
set "Destination=C:\Files-Parsed"
for /f "delims=" %%a in (%filelist%) do (
pushd %source%
for /f "delims=" %%i in ('dir /s /b /ad "%%a*"') do xcopy /E /F /D "%%~fi" "%Destination%"
popd
)
after getting the entry in the file, for /d will do a directory listing of the directory* in the source directory and physically copy the dir as C:\source\*\ABC2018 etc.

Win 10 forfiles searchmask

I have downloaded a series of torrent files, and want to delete some of the attached files that came within them. I'm familiar with forfiles but don't know how to set up a searchmask from a file of unwanted extensions (i.e. *.jpg, *.txt, etc.).
So far, I have captured 17 extensions that I won't ever need, and would hate to have to loop the entire batch program for an eighteenth time if I find another.
First, prepare a list of all the unwanted files with dir /b /s, capturing the output into a temporary text file;
dir /b /s *.txt /s *.jpg /s *.etc >%temp%\unwanted.lst
see help dir for an explanation on the /b and /s switches.
Then, delete the files in the list with a simple for /f over the contents of the captured list
for /f "delims=" %%a in (%temp%\unwanted.lst) do del %%a
See help for to understand what the /f does.
So, putting all the pieces together, your batch file would be something similar to this one:
#echo off
set "otf=%temp%\unwanted-%random%.lst"
dir /b /s *.jpg /s *.txt /s *.etc >%otf%
for /f %%a in (%otf%) do echo del "%%a"
echo del %otf%
note that it uses the %random% pseudovariable to minimize the risk of collisions
test in your situation and remove the echo commands

How to delete directories containing certain string using batch file in Windows?

From
How to delete files containing certain string using batch file in Windows?
I learned how to mass delete files which contain certain strings. What I did is
del *(2)* /f /s
but this did not delete directories. It only delete files.
How can I also mass-delete directories which contain certain strings?
There's no standard Windows command to delete files and directories on the same level. DEL is used for files, RMDIR / RD is used for directories (however it can delete files within directories).
RMDIR / RD does not work with wildcards, so you need to use a FOR loop. As it is, the below code will print out the commandos to delete the directories in your question. Remove the ECHO when you're confident the deletion will do what you want.
#ECHO OFF
FOR /F "tokens=*" %%G IN ('DIR /B /AD /S "*(2)*"') DO (
ECHO RMDIR /S /Q "%%G"
)
You can also reduce this to a one-liner...
FOR /F "tokens=*" %%G IN ('DIR /B /AD /S "*(2)*"') DO ECHO RMDIR /S /Q "%%G"
...and if you want to execute it directly in the shell (as opposed to from a .bat file), do:
FOR /F "tokens=*" %G IN ('DIR /B /AD /S "*(2)*"') DO ECHO RMDIR /S /Q "%G"
Flags explanation:
FOR
/F: iterate over a fileset
DIR
/B: bare format (needed so it works with FOR)
/AD: filter for directories
/S: work recursive
RMDIR
/S: work recursive
/Q: quiet mode

How to delete specific hidden sub folders recursively with batch

A bad program left several ".data" hidden folders in every sub directory.
I have tried this so far, and it doesn't work...
RD /ah /s /q "D:\This Folder\.data"
I have many folders and sub-folders, each one has a ".data" hidden folder there that I would like to remove. If it helps, I would like to delete all the hidden folders because they are all ".data".
#echo off
setlocal enableextensions disabledelayedexpansion
for /f "delims= eol=" %%a in ('
dir /adh /s /b 2^>nul
') do if /i "%%~nxa"==".data" echo rd /s /q "%%~fa"
This executes a dir command to retrieve a recursive (/s) list of hidden directories (/ahd) in bare format (/b). This list is processed by a for /f command that will check for each match if it is a .data folder. If it matches the condition, the folder and its contents are removed.
note the rd commands are only echoed to console. If the output is correct, remove the echo command.
#ECHO OFF
for /f "tokens=*" %%F in ('dir /s /b /o:n /adh') do (
if "%%~nxF"==".data" rd /s /q "%%F"
)
dir /s /b /o:n /adh gives you all folders and subfolders skipping files. for /f iterates over all these folders. %%~nxF extracts the last folder name from the whole path so we can check whether it is .data. If this is the case rd /s /q %%F deletes the folder.
Read what you get when you type for /? then:
Use the /d
FOR /D %variable IN (set) DO command [command-parameters]
If set contains wildcards, then specifies to match against directory
names instead of file names.
and the /r switches together
FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]
Walks the directory tree rooted at [drive:]path, executing the FOR
statement in each directory of the tree. If no directory
specification is specified after /R then the current directory is
assumed. If set is just a single period (.) character then it
will just enumerate the directory tree.
To scan folders recursivly
for /d /r . %%a in (data*) do rd /s /q "%%a"

batch file picking up files from directory and subdirectories

I have a batch file that is appending a date to all to the file name of all CSV files. I only want CSV files in one directory to be picked up and no subdirectories. It appears to be running through all subdirectories though.
I have this code currently in the batch file
:: copy files
For /f "delims=" %%a in ('Dir /A:-D /b /s "%LOCALDIR%"*.csv 2^>nul') do If exist "%%a" (
COPY "%%a" "%LOCALDIR%%dtt%-%%~na.csv"
DEL "%%a"
)
I have tried getting rid of the /s in the code but then no files are picked up in the directory I want to look for.
Any help is greatly appreciated.
Why not just use a simple loop like?
pushd %LOCALDIR%
for %%A in (*.csv) do ren "%%~A" "%dtt%-%%~A"
popd
Or for a one liner
for %%A in (%LOCALDIR%\*.csv) do ren "%%~A" "%dtt%-%%~A"
If the path has spaces in it remember to use the usebackq option.

Resources