ashish g
ashish g

Reputation: 291

Batch File to delete files from the folder whose filenames not mentioned in the XML file

Working on a batch file from past 2 days but no luck :(

I am in need of a script (Bat) that will delete all projects (mostly vb) not mentioned in my build list (xml).

Eg: I have a folder named C:\123 which has around 15 files in it. I have a build list (XML file) which has 10 filenames which are there in C:\123. Now I want a batch file script which will delete the rest 5 files from C:\123 which is not there in the xml file.

Any help will be greatly appreciated! script tried to compare 2 folders and delete the identical files first (thought of tweaking it later according to my need but this didn't work)

@ECHO OFF

SET LOCALFOLDER=C:\123
SET OTHERFOLDER=D:\123

Pause
:LOCALKEYTEMP

SET FILE=DONE

:: THIS LINE SCANS THE LOCAL FOLDER FOR FILES, 
:: WE CAN USE THIS TO SCAN SEPERATE FILES ONE AT A 

TIME

FOR /F "TOKENS=*" %%G IN ('DIR/B ^"%LOCALFOLDER%

\*.*^"') DO SET FILE=%%G
Pause
ECHO %FILE%
pause

IF %FILE%==DONE GOTO END
pause

ECHO N|COMP "%LOCALFOLDER%\%FILE%" 

"%OTHERFOLDER%\%FILE%" | FIND "FILES COMPARE OK" > 

NUL
pause

IF ERRORLEVEL 1 GOTO DIFFERENTKEYS
IF ERRORLEVEL 0 GOTO DELETEBOTH
pause

DELETEBOTH

DEL /Q "%LOCALFOLDER%\%FILE%"
DEL /Q "%OTHERFOLDER%\%FILE%"

GOTO LOCALKEYTEMP


DIFFERENTKEYS

:: THIS LINE DELETES THE LOCAL FOLDERS FILES WHICH IS 

NECCESSARY FOR THIS SCRIPT

DEL /Q "%LOCALFOLDER%\%FILE%"

GOTO LOCALKEYTEMP


:END
ECHO ALL FILES SHOULD BE DELETED FROM 

%LOCALFOLDER%
pause
ECHO ALL DIFFERENT FILES SHOULD BE LEFT ON 

%OTHERFOLDER%
PAUSE

EXIT

xml file looks like:

<ProjectsToBuild>
    <Project>C:\123\Clients\Direct\App1.vbproj</Project>                      
    <Project>C:\123\Clients\Direct\App2.vbproj</Project> 
</ProjectsToBuild>

result of the Aacini's Batch Script:

Press any key to continue . . .
List of existent files:
fileName[D:\123\Subfolder1\a1.txt.txt]=1
fileName[D:\123\Subfolder1\a2.txt.txt]=1
fileName[D:\123\Subfolder1\a3.txt.txt]=1
fileName[D:\123\Subfolder1\a4.txt.txt]=1
fileName[D:\123\Subfolder1\buildList.xml]=1
fileName[D:\123\Subfolder2\a1.txt.txt]=1
fileName[D:\123\Subfolder2\a2.txt.txt]=1
fileName[D:\123\Subfolder2\a3.txt.txt]=1
fileName[D:\123\Subfolder2\a4.txt.txt]=1
fileName[D:\123\Subfolder2\buildList.xml]=1
Press any key to continue . . .

Keep these files:
Press any key to continue . . .

Remove these files:
Press any key to continue . . .
Press any key to continue . . .

This deletes all the files in the folder.

Upvotes: 0

Views: 1931

Answers (1)

Aacini
Aacini

Reputation: 67216

Excuse me. In your question you said "I have a folder named C:\123 which has around 15 files in it"; however, the build list just have two names that are in folders two levels down below C:\123. Indeed, C:\123 folder have no one file mentioned in build list. I assumed that you want to delete all files in C:\123 at any level that are not mentioned in the build list (because the build list mention files two levels down below C:\123). If this is not what you want, then the Batch file must be modified.

@echo off
setlocal
rem Create a list of existent file names in C:\123 *at any level*
for /R "C:\123" %%a in (*.*) do set "fileName[%%a]=1"
ECHO List of existent files:
SET fileName[
ECHO/
rem Process the build list and remove found names from existent files list
ECHO Keep these files:
for /F "tokens=2-4 delims=<>" %%a in (buildList.xml) do (
   if "%%a" == "Project" if "%%c" == "/Project" (
      ECHO fileName[%%b]
      set "fileName[%%b]="
   )
)
ECHO/
ECHO Remove these files:
rem Remove the remaining files
for /F "tokens=2 delims=[]" %%a in ('set fileName[') do (
   ECHO del "%%a"
)

Upvotes: 1

Related Questions