Mintu
Mintu

Reputation: 1

Copy subfolders with exclusions

I have a messy situation on production server, there is a location on production which should only contain 3 folder however due to not following some process we have got more than 1000 folders and files and I am aiming to clean it via a batch file so that there is no chances of human error.

So I would like to copy all folders and files except 3 folders to a new location. can someone help in this as not able to put logic to exclude these 3 folders.

Upvotes: 0

Views: 51

Answers (2)

foxidrive
foxidrive

Reputation: 41234

This writes tempmove.bat.txt in the same folder as the batch file that contains the move commands to move every folder except the three folders shown (testenv stageenv prodenv).

You can examine the text file before renaming it to .bat to actually use, if it shows the right commands.

Make sure "d:\wrong folders" folder already exists.

@echo off
cd /d "c:\production folder"
(
for /d %%a in (*) do (
  if /i not "%%~nxa"=="testenv" if /i not "%%~nxa"=="stageenv" if /i not "%%~nxa"=="prodenv" echo move "%%~fa" "d:\wrong folders\%%~nxa"
)
)>"%~dp0\tempmove.bat.txt"
pause

Upvotes: 0

jpf
jpf

Reputation: 1475

Create a file called ex.txt that includes 3 lines, each of which is the name of the folder that you would like to exclude from the copy, e.g.:

\folder1\
\folder2\
\folder3\

Now, go to the parent of the high-level directory (say, directory_to_copy), that you would like to copy, in which location exists the ex.txt file, and type

xcopy /e /i /exclude:ex.txt directory_to_copy destination_name

This will exclude the folders folder1, folder2, and folder3 from the copy.

Note: the backslashes \ are important to ensure that the other folders containing those strings (folder1, folder2, and folder3) are not excluded.

Upvotes: 1

Related Questions