| 1. |
Solve : Comparing two directories? |
|
Answer» I want to be able to compare two directories from a command line like if I wrote a batch file for copying a whole directory and then comparing them. I have found "comp" and "fc"but those only compare specific files and there are a TON of files in the directories I want to compare. Is there a command to be able to compare two directories and all their sub directories? Quote I understand that the "for statement" is for, for "given code" do "this." But could you explain "tokens=* delims=" %%v in ('dir c:\FolderA\*.* /b'), specifically tokens, delims and what the %%v is doing? tokens=* TELLS the interpreter there is a single variable (%%v) and all the data on each line from the dir command is to be stuffed into that variable delims=" overrides the DEFAULT delimiters which are space and tab If you run the dir command from the prompt, you'll see the output appears as: filename1.ext filename2.ext file name3.ext . . . By overriding the default delimiters and and declaring a single variable, each iteration of the for loop PUTS a filename into %%v (including filenames with embedded spaces) Quote If their are files in both we get the first message but if it does not we get the second message, will this work all the way through the directory tree? Nope; as written compares folderA to folderB, then FolderB to FolderA. Did you check out rSync? Might be a better solution with recursed directories. Good luck, rsync might work but sense this is a network server and not a personal computer for myself I would rather not install anything new. Plus this is not sync per say. I am copying the files from one location to another, confirming they are there, and then deleting the original directory. The copy is easy I just use xcopy xcopy C:\FolderA\*.* G:\ /E Which copies everything from the first directory to the second directory including all sub directories. Then after a little manipulation I will run your code to compare the two directories before the deletion. My next question is if I use how do I delete everything below a certain point in the directory? I have tried del but it wants a specific file and I have tried rmdir but it deletes that directory and I want to keep it just remove everything below it so in the end it is a empty directory. Could I do something like this? for /f "tokens=* delims=" %%v in ('dir c:\FolderA\*.* /b') do (del "c:\FolderA\%%v ) Thanks for your help! Quote My next question is if I use how do I delete everything below a certain point in the directory? Quote Could I do something like this? Del works only with files. RD works with FOLDERS. One way to approach this would be to get the collection of files in FolderA and delete them. Then go back and get the collection of folders in FolderA and remove the sub-folders and their files. Code: [Select]for /f "tokens=* delims=" %%v in ('dir c:\FolderA\*.* /a:-d /b') do ( del "c:\FolderA\%%v ) for /f "tokens=* delims=" %%v in ('dir c:\FolderA\*.* /a:d /b') do ( rd "c:\FolderA\%%v /s /q ) The snippet should leave you with an empty FolderA. I would encourage you to test this before committing to production. Good luck. There seems to be a massive fascination with batch files this week. Must be something in the water.Thanks! I did do a bit of testing with some random files and folders I created myself but here is the some what finished product. @echo off set /p inputdir=Which directory are you moving files from (Collateral, Design, or Production)? echo. ::If directory is not found it goes to the DirNotFound section if not exist "F:\macdata\MonthlyDisks\CurrentHOData\%inputdir%" goto :DirNotFound ::If the directory is found it will execute the xcopy command if exist "F:\macdata\MonthlyDisks\CurrentHOData\%inputdir%" xcopy "F:\macdata\MonthlyDisks\CurrentHOData\%inputdir%\*.*" G:\ /E pause ::Comparing the two directories to make sure both match for the files that have moved for /f "tokens=* delims=" %%v in ('dir F:\macdata\MonthlyDisks\CurrentHOData\%inputdir% /b') do ( if exist G:\%%v echo File %%v in %inputdir%; File %%v in G: ::If they do not both match it will go to the exit section if not exist G:\%%v goto :exit ) pause ::Deleteing the old directory and files for /f "tokens=* delims=" %%v in ('dir F:\macdata\MonthlyDisks\CurrentHOData\%inputdir%\*.* /a:-d /b') do ( del "F:\macdata\MonthlyDisks\CurrentHOData\%inputdir%\%%v" ) for /f "tokens=* delims=" %%v in ('dir F:\macdata\MonthlyDisks\CurrentHOData\%inputdir%\*.* /a:d /b') do ( rd "F:\macdata\MonthlyDisks\CurrentHOData\%inputdir%\%%v" /s /q ) echo The orginal files have finshed being moved and deleted. pause EXIT :DirNotFound echo ***That directory does not exist*** @ping 127.0.0.1 -n 5 -w 1000 > nul pause :exit echo File %%v in %inputdir%; File %%v NOT in G: pause exit It's really basic right now and I am sure I will do further tweaking but it gets the job done and makes sure it's done easier and right.folders...files... Quote from: Sidewinder on July 17, 2008, 04:18:04 PM
i like this code, simple and do not have to install anything. but can this code compare everything in the parent folder that i specified ex. FolderA, has 4 folders, and those may have other folders in them and other files. this only compares the INITIAL structure in FolderA (in my case 8 folders, and nothing in those folders.) thank you, jat but take note that it only checks for file existence and not file contents or file modified. if the source is older than destination, the destination will be overwritten with an old file. Best to check for md5 hash and file modified date.Quote this only compares the initial structure in FolderA (in my case 8 folders, and nothing in those folders.)[ Yes, you're right. 1. That's all it was designed to do otherwise it wouldn't conform to the KISS method of coding. 2. Each situation is different, which is why we frown on hijacking threads. Not sure what you asking, but if you want to check sub folders, AND the sub folder structure of FolderA was identical to FolderB, you could do a search and replace of FolderA with FolderB in the path and check that way. Otherwise you'd have to gather a list of all the folders in FolderB (including the top level FolderB itself), and check whether each file from FolderA (including subfolders) exists in any of the FolderB structure. Duplicate file names in different FolderB folders could really foul up the validity of your results. Finally you'd have to flip all the logic so FolderB is the master and FolderA is the slave. Dusty brings up a good point. Files with identical names do not necessarily have identical content. You didn't mention your OS but there is a PowerToy called SyncToy that may help. You don't actually have to sync anything, it has preview function. XP, VISTA only. If the folders in question have a lot of files and/or copying permissions is also a must, you might want to consider robocopy (microsoft) instead of xcopy. It is a lot more powerful copy tool than xcopy, it is a 32 bit app while xcopy is a 16 bit dos app and it is also free. Depending what you want to do you can use command line switches like /mov, /mir, (/copy is the default one), etc. You can see what it can do and how to use it - here: http://www.ss64.com/nt/robocopy.html and you can download it from here: http://www.microsoft.com/downloads/details.aspx?familyid=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en Geza Quote from: Sidewinder on July 17, 2008, 04:18:04 PM It's déjà vu all over again. I posted this in another thread, but it may help you out too: This works great! Anybody have any idea what command I would use to list only files found in FolderA and FolderB for today's date only? Thanks.You are posting in a 4 year old thread. I suggest you start a new topic if you have a question. |
|