Batch file to compare files from different folders and delete identical files
Discuss Batch file to compare files from different folders and delete identical files in the Programming forum on Dev Hardware. Batch file to compare files from different folders and delete identical files Programming forum discussing administration and coding scripts in languages including PHP, C++, C, Perl, Java, Visual Basic, XML, HTML, CGI, ActionScript, CSS, JavaScript, Fortran, BASH, and issues related to databases and even batch files.
Posts: 11
Time spent in forums: 2 h 6 m 41 sec
Reputation Power: 0
Batch file to compare files from different folders and delete identical files
Hey there, I am not new to batch file scripting, but I am very basic and still learning
I was hoping someone could give me some advice on how to process this in a batch file:
1)Scan and Compare 2 seperate folders for identical files
2)Delete the identical files from both folders,
3)it must not delete unidentical files
3)It also needs to process more than one file, if it finds more than 1 file
4)It has to be a batch file, for automated scripting on my network
its for windows xp too
So far I have been able to find identical files in the 2 folders, however I cant do much else
This is my batch file so far, edited for simplicity of the forum
=============================
SET LOCALFOLDER=C:\123
SET OTHERFOLDER=D:\123
You can also test this by making the 2 folders c:\123 and d\123 and copying an identical file into both, or changing the
SET folderlocations, to test it out if you have time.
Thanks for all the help anyone can offer, I appreciate it in advance
Posts: 11
Time spent in forums: 2 h 6 m 41 sec
Reputation Power: 0
Well, after about 4 hours of painfully searching for 3rd party tools, which were annoying shareware rubbish which popped up tiral messages and paused the prompt, i finally copied the for command from http://www.ss64.com/nt/for.html and modified it to work for me, so I wrote a batch file that worked, ah well sorry for wasting your guys time, maybe someone might come across this one day as the same problem as me
thanks guys
cya
@ECHO OFF
SET LOCALFOLDER=C:\123
SET OTHERFOLDER=D:\123
SET TEMPLOG=C:\TEMP.LOG
Posts: 11
Time spent in forums: 2 h 6 m 41 sec
Reputation Power: 0
Still no one has replied heh, ah well, examing the last batch i wrote, it wasnt perfect, actually if you have 5 files for example and 4 of them are equal ie hi.1, hi.2, hi.3, hi.4, hi.5, but one of them was different ie hi.3, it would still delete hi.3 when it comp-ared the other files and got a positive.
i re wrote this also copied and edited from ss64.com again, this time i studied a bit harder and found that this could set a variable to a single file, then i can compare that variable and if its equal delete both, if not just delete the local file
this script works what i want, still messy i think but hey it works lol
@ECHO OFF
SET LOCALFOLDER=C:\123
SET OTHERFOLDER=D:\123
: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
ECHO %FILE%
Posts: 4,206
Time spent in forums: 4 Months 2 Days 2 h 40 m 23 sec
Reputation Power: 7051
Do you really need a batch file for that? Search for google term 'Duplicate File Finder' and web will present to you more than sufficient freeware solutions!
Edit: And don't assume people will respond within an hour or two after you posted a question. It is still Sunday here and most of people are enjoying the last moments of the weekend.
Posts: 11
Time spent in forums: 2 h 6 m 41 sec
Reputation Power: 0
Thanks for the reply
Thanks for the reply. I need to script this via command-line because we have 30 gaming pcs which are always creating duplicates, so it has to run via batch file, the game shortcuts are pointing to a batch file so they can load defaults retrieve unique cd keys and all that obviously it needs to be automatic and in the background, also searching Duplicate File Finder command-line didnt yield much fruit on google, neither was i expecting it to. Anyway i hope in the future people will be able to help me out with other problems.
Posts: 4,206
Time spent in forums: 4 Months 2 Days 2 h 40 m 23 sec
Reputation Power: 7051
We have some really good people here who know lot of batch programming. As I said, since it was a weekend, I won't expect them to be reading and replying each and every thread. I am sure tomorrow, as it will be Monday, they will reply, because they are always hunting for such threads.
Posts: 5,976
Time spent in forums: 1 Month 2 Weeks 19 h 1 m
Reputation Power: 13055
Gee, would I be one of those people, Tejas?
Ndog, does this bave to be a DOS batch file, or can you use a WSH script instead. WSH is much more versatile and powerful and since it's native to Windows XP, I don't see why you couldn't use it instead.
__________________ Don't like me? Click it.
Scripting problems? Windows questions? Ask the Windows Guru!
Posts: 11
Time spent in forums: 2 h 6 m 41 sec
Reputation Power: 0
Ok
Well WSH sounds daunting, i am still trying to figure out batch files and still cant do everything, learning a new programming language sounds hard, however if you can re direct me to a site for newbies then i will have a look around, thanks
Posts: 11
Time spent in forums: 2 h 6 m 41 sec
Reputation Power: 0
8.3 probs in xp lol
Sorry to be of further disturbance, but it doesnt support 'spaced out file names' ie hi there.txt, compared to hithere.txt.
I tried using DIR /B /X but that doesnt set it to 8.3 filenames either. anyone know if theres a way to do that, only display basic information (ie 'dir /b' 1 line of information) and in 8.3 format?
Posts: 11
Time spent in forums: 2 h 6 m 41 sec
Reputation Power: 0
Okay, this is the final edit of this script, I finally managed to sort it out, there were 2 major bugs in it, the comp | find command wasnt outputting the errorlevel correctly, and the loop if file==done cant work because it cant work with LFN, so i had to set file="%file%", and it works now.
Simple as script, I reckon about 90% of work is killing bugs lol
@ECHO OFF
:: Compares identical files from 2 folders
:: Deletes the identical ones
:: Deletes all the local ones
SET LOCALFOLDER=C:\123
SET OTHERFOLDER=D:\123
:LOCALKEYTEMP
:: THIS LINE SCANS THE LOCAL FOLDER FOR FILES,
:: WE CAN USE THIS TO SCAN SEPERATE FILES ONE AT A TIME
SET FILE=DONE
FOR /F "TOKENS=*" %%G IN ('DIR/B ^%LOCALFOLDER%\*.*^') DO SET FILE=%%G
SET FULLPATHLOCAL="%LOCALFOLDER%\%FILE%"
SET FULLPATHOTHER="%OTHERFOLDER%\%FILE%"
SET FILE="%FILE%"
IF "DONE"==%FILE% GOTO END
ECHO N|COMP %FULLPATHLOCAL% %FULLPATHOTHER%
IF ERRORLEVEL 1 GOTO DIFFERENTKEYS
IF ERRORLEVEL 0 GOTO DELETEBOTH
ELETEBOTH
DEL /Q %FULLPATHLOCAL%
DEL /Q %FULLPATHOTHER%
GOTO LOCALKEYTEMP
IFFERENTKEYS
:: THIS LINE DELETES THE LOCAL FOLDERS FILES WHICH IS NECCESSARY FOR THIS SCRIPT
DEL /Q %FULLPATHLOCAL%
GOTO LOCALKEYTEMP
:END
ECHO ALL FILES DELETED IN %LOCALFOLDER%
ECHO ALL NON IDENTICAL FILES REMAIN ON %OTHERFOLDER%
PING -n 6 127.0.0.1>nul
CLS
EXIT
to all those who have followed this thread, really though I am looking for a real batch file scripting forum, this was the only thing I could find, I have a LOT of projects to work on, so I need help with experienced people, thanks to all the guys who commented, it was good
Location: Centrally located in the middle of nowhere, CA
Posts: 9,295
Time spent in forums: 10 Months 3 Weeks 3 Days 2 h 8 m 48 sec
Reputation Power: 17891
Quote:
Originally Posted by NDog
... to all those who have followed this thread, really though I am looking for a real batch file scripting forum, this was the only thing I could find, I have a LOT of projects to work on, so I need help with experienced people, thanks to all the guys who commented, it was good
You could give our sister sites, DevShed and ASP Free a looksee... they are both programming forums. DevShed in particular covers a wide variety of programming languages including scripts.
"In front of a monitor is a dangerous place from which to view the world." --Terri Wells
Enable BSOD: Control Panel/Systems, Advanced Tab, hit the Settings button under Startup and Recovery, and under the System Failure area, uncheck the Automatically Restart checkbox.
Posts: 5,976
Time spent in forums: 1 Month 2 Weeks 19 h 1 m
Reputation Power: 13055
Quote:
Originally Posted by NDog
Well WSH sounds daunting, i am still trying to figure out batch files and still cant do everything, learning a new programming language sounds hard, however if you can re direct me to a site for newbies then i will have a look around, thanks
I apologize for not responding to this comment sooner. I've been under a lot of time constraints lately.
Batch is limited in it's functionality. It's native functions are limited to a few supported commands. To make batch effective in many cases, you have to add third-party components. It also lacks in its ability to perform logical expressions and in its ability to handle user interactivity. The list of limitations goes on.
Windows Script Host (or WSH) is a scripting environment. Since it uses scripting languages you have the full arsenal of logic statements, loops, arrays, etc. WSH is integrated into the system therefore is much more powerful by design.
Think of batch like a small macro. When you run it, it performs a specific task, and is useless for anything else. WSH, on the other hand is like a miniature program. It's abilities are only limited by your programming creativity.
You can use any scripting language installed on the machine. It supports VBScript and JScript by default.