Cross referencing folders

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
supersoup
Posts: 5
Joined: 10 Jul 2009 07:35

Cross referencing folders

#1 Post by supersoup » 10 Jul 2009 12:16

I am trying to write a program that deletes files from FolderA that are already in FolderB, but i am new to dos commands.

I have written 2 batch files thinking that this was the correct direction to go in but im not really sure.

Code: Select all

::Creates a text file called "Folder A Contents.txt" that contains all the files in Folder A

echo Contents of Folder A >> "Folder A Contents.txt"
dir "C:\My Documents\Folder A" /s /b >> "Folder A Contents.txt"


Code: Select all

::Creates "IF Test Results.txt" which informs if C:\My Documents\Folder A\File A.txt exists

echo off
if not exist "C:\My Documents\Folder A\File A.txt" (
   echo The file C:\My Documents\Folder A\File A.txt DOES NOT exist. >> "IF Test Results.txt"
) ELSE (
   echo The file C:\My Documents\Folder A\File A.txt DOES exist. >> "IF Test Results.txt"
)
:END

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 10 Jul 2009 21:33

First let me say that robocopy is a nice utility from Microsoft (part of the Windows 2003 server resource kit) that would be able to do this directly (though the proper command line options are tricky).

But anyway:

Code: Select all

@echo off

setlocal enabledelayedexpansion

set "sourcedir=c:\path to\FolderA"
set "deldir=c:\path to\FolderB"

for /f "usebackq tokens=* delims=" %%a in (`dir /a-d "%sourcedir%" /b /s`) do (
   set "filetest=%%~a"
   del "!filetest:%sourcedir%=%deldir%!" >nul 2>nul
)

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#3 Post by ghostmachine4 » 11 Jul 2009 20:31

@OP, does your files in FolderA and FolderB have the same directory structure? anyway, here's a vbscript. It doesn't assume you have same directory structure. ie, if a file anywhere under the directory FolderA exists already in FolderB (also anywhere) , then it will delete the file

Code: Select all

Set objFS = CreateObject("Scripting.FileSystemObject")
Set d = CreateObject("Scripting.Dictionary")
strFolderA = "c:\test"
strFolderB = "c:\tmp\"
Set objFolder = objFS.GetFolder(strFolderA)
Set objFolder1 = objFS.GetFolder(strFolderB)
Go( objFolder)
Gocheck( objFolder1)

Sub Go(objDIR)
  If objDIR <> "\System Volume Information" Then
    For Each eFolder in objDIR.SubFolders
         Go eFolder
    Next
   For Each strFile In objFS.GetFolder(objDIR).Files
      strFileName = strFile.Name
      strFileParent = strFile.ParentFolder
      strFilePath = strFile.Path
      'collect all files and their full paths.
      d.Add strFilePath , strFileName
   Next
  End If 
End Sub

Sub Gocheck(objDIR1)
   e = d.Items
   If  objDIR1 <> "\System Volume Information" Then
       For Each eFolder in objDIR1.SubFolders
            Gocheck eFolder
       Next
      For Each strFile In objFS.GetFolder(objDIR1).Files
         strFileName = strFile.Name
         strFileParent = strFile.ParentFolder
         strFilePath = strFile.Path
         For Each strKey in e
             If strFileName = strKey Then
                WScript.Echo "Found : " & strKey,strFilePath
                'remove file
                objFS.DeleteFile(strFilePath)
             End If
         Next
      Next
     End If 
End Sub


supersoup
Posts: 5
Joined: 10 Jul 2009 07:35

#4 Post by supersoup » 13 Jul 2009 06:35

yes the 2 folders have similar directory structure.

could you please explain how i use this code?
or how it works?

supersoup
Posts: 5
Joined: 10 Jul 2009 07:35

#5 Post by supersoup » 13 Jul 2009 07:09

I got it to work!
Thank you so much!

The only problem is it only deletes 1 file each time it runs, how do i fix this?
I already know the file path of the folders i want to delete from, in case that helps

It also gives me an Error:

Line: 38
Char: 17
Error: File not found

Post Reply