3

I have a running Magento 2.x website's folder structure in my Windows 7 64-Bit system, at C:\UwAmp\www\magento2root path.

Now this Magento root folder contains following directory-subdirectory pairs as direct children(eg. [magento2root]/var/cache):

var/tmp
var/cache
var/page_cache
var/generation
var/view_preprocessed
var/composer_home
var/log
var/report
var/session
pub/static

Now some of the above paths contains .htaccess(eg. pub/static/.htaccess) file, which 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.

Below are commands I tried putting in script(for pub/static) but they delete .htaccess as well:

del /q "%cd%/pub/static/.htaccess/*"
FOR /D %%p IN ("%cd%/pub/static/.htaccess"/*.*) DO rmdir "%%p" /s /q

As I am not much of a Windows Batch scripter, I am doing this process manually, can someone help me out with script ?

1

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 .htaccess files.


Batch Script

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.

@ECHO ON

FOR %%A IN (test,site1,site2\folder,pub\static) DO (CALL :routine "%%~A")
GOTO :EOF

:routine
SET SrcDir=C:\UwAmp\www\magento2root\%~1
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%"
GOTO :EOF

Further Resources

  • CALL
  • FOR
  • FOR /F

    • 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
    
  • DIR

    /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

  • IF

  • DEL
  • Robocopy

    /S :: copy Subdirectories, but not empty ones.

    /MOVE :: MOVE files AND dirs (delete from source after copying).

  • MD


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.

@ECHO ON

::SET array=1,2,3,4,"5","6","7/7","8"
::SET array=1,2,3,4,5,6,7/7,8
::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
PAUSE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.