Home > Software design >  Windows batch to remove folders that aren't in a text list
Windows batch to remove folders that aren't in a text list

Time:10-04

I'm trying to create a script that copies and updates files into a folder based on what is found in a text doc. Since this list updates regularly, I'd like for the script to also remove any folders that are no longer present in the list as well.

The playlist.txt doc contains file paths:

G:\path\sample1.jpg
G:\path\sample2.jpg
G:\path\sample2.jpg

Here's the script that I have so far that successfully copies the files as well any companion files from their original directory to the new one

\\\\, @echo off

setlocal enabledelayedexpansion

:START

cls

set strmLibraryDrive=\\NAME\g\
set strmLibraryPath=Sample\Path

FOR /F "eol= tokens=1 delims=" %%E IN (playlist.txt) DO (

    set originalLibraryDrive=%%~dE
    set originalLibraryPath=%%~pE
    set originalFileName=%%~nE
    set originalFileExt=%%~xE

    echo    Original Library Path   : !originalLibraryDrive!!originalLibraryPath!
    echo    Original File Name  : !originalFileName!
    echo    original File Ext   : !originalFileExt!
    echo    strm Library Path   : !strmLibraryDrive!!strmLibraryPath!

    xcopy "!originalLibraryDrive!!originalLibraryPath!\!originalFileName!.*" "!strmLibraryDrive!!strmLibraryPath!\!originalFileName!\" /D /E /C /Q /H /R /Y /K 
    
    xcopy "!originalLibraryDrive!!originalLibraryPath!\!originalFileName!*.*" "!strmLibraryDrive!!strmLibraryPath!\!originalFileName!\" /D /E /C /Q /H /R /Y /K 
            
            )

:END
echo.
echo Finished.
echo.

I don't really know what I'm doing so any help is appreciated.

CodePudding user response:

set list=C:\your\list.txt
set folder=C:\your\path


dir /b "%folder%" > getfiles.txt
for /f "delims== tokens=1,2" %%G in (%list%) do (type getfiles.txt | findstr /l /v "%%G" >> delete.txt)
for /f "delims== tokens=1,2" %%H in (delete.txt) do (rmdir /S /Q "%folder%\%%H")
del delete.txt
del getfiles.txt
  • Related