I want to keep and I need to remove all the content(files, subfolders, sub-subfolders etc.) besides their relevant .htaccess file of all the mentioned paths, through a single script.
You can use a
FOR /F loop and a
DIR command with the
/A-D switch to exclude folders and iterate through a directory recursively for
*.*. You can use conditional
IF logic to say if the iterated file per the
DIR command equals a name of
.htaccess, then do not remove that file. Otherwise, it will proceed to
DEL files with other names.
Now that the file cleanup is complete, you can use
Robocopy with the source and destination paths being the same which at this point should be all except for the ones containing the
Important: Be sure to test this out from a test location to ensure it'll suffice before running against production systems.
Note: The top
FOR loop (
%%A) will be the list of subfolder names\paths beneath the parent level folders of the main directory which you only want the cleanup to apply to. You can use the
CALL command and pass the iterated value as the first argument to the routine beneath that will then set the final full path of the folder locations to run the clean against recursively.
FOR %%A IN (test,site1,site2\folder,pub\static) DO (CALL :routine "%%~A")
FOR /F "TOKENS=*" %%p IN ('DIR /S /B /A-D "%SrcDir%\*.*"') DO IF NOT [%%~nxp]==[.htaccess] DEL /Q /F "%%~p"
ROBOCOPY "%SrcDir%" "%SrcDir%" /S /MOVE
IF NOT EXIST "%SrcDir%" MD "%SrcDir%"
- Batch Substitutions (FOR /?)
In addition, substitution of FOR variable references has been
enhanced. You can now use the following optional syntax:
%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
/A Displays files with specified attributes.
attributes D Directories R Read-only files
H Hidden files A Files ready for archiving
S System files I Not content indexed files
L Reparse Points - Prefix meaning not
/S :: copy Subdirectories, but not empty ones.
/MOVE :: MOVE files AND dirs (delete from source after copying).
Bonus Array Object as Variable in Batch
You can put the array variable in various formats to then use as the
(set) portion value of a
FOR loop(examples below). You should test the format you choose and confirm each iterated value returns correctly with the
ECHO command as in my example before using for other commands. I've commented out a few of the variations below but I'm sure there are other methods but this is only bonus material of this method in general.
Furthermore, I have an answer on copy array in a batch file for another potential method you could use to emulate an array with indexing and iterating subsequent values accordingly and using standards for subroutine variables that are set before correlated commands to process.
::SET array=1 2 3 4 5 6 7/7 8
SET array=1 2 3 4 5 6 "7 / 7" 8
FOR %%A IN (%array%) DO ECHO %%~A