Remove Duplicate Files and Move them in the Bin.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
mingolito
Posts: 28
Joined: 04 Dec 2014 11:34

Remove Duplicate Files and Move them in the Bin.

#1 Post by mingolito » 16 Jun 2015 08:31

Mod Edit: This doesn't compare the contents of files

Hello guys, use this command batch to remove duplicate files in a folder and its subfolders:
SET "StartFolder=."

FOR /r "%StartFolder%" %%i IN (*) DO call:doit "%%~i"

GOTO:EOF

:doit
SET "fname=%~nx1"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "fsize=!$%fname%!"
IF DEFINED $!fname! (
ENDLOCAL
IF "%fsize%"=="%~z1" DEL "%~1"
) ELSE (
ENDLOCAL
SET "$%~nx1=%~z1"
)

GOTO:EOF

I wanted to ask for help in the ability to modify the command, moving the deleted files into the bin, and not permanently.
So that it if necessary can be recovered in the bin.
Thankss.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Remove Duplicate Files and Move them in the Bin.

#2 Post by foxidrive » 16 Jun 2015 14:55

Third party tools exist that will delete to the recycle bin, rather than using the DEL command.

mingolito
Posts: 28
Joined: 04 Dec 2014 11:34

Re: Remove Duplicate Files and Move them in the Bin.

#3 Post by mingolito » 17 Jun 2015 04:37

foxidrive wrote:Third party tools exist that will delete to the recycle bin, rather than using the DEL command.

Thanks for the answer, but how do I adapt it to the code posted.
would you help me ???

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Remove Duplicate Files and Move them in the Bin.

#4 Post by ShadowThief » 17 Jun 2015 05:56

mingolito wrote:
foxidrive wrote:Third party tools exist that will delete to the recycle bin, rather than using the DEL command.

Thanks for the answer, but how do I adapt it to the code posted.
would you help me ???

Find one that has a command line version and replace DEL with that.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Remove Duplicate Files and Move them in the Bin.

#5 Post by foxidrive » 17 Jun 2015 06:41

Here is a post with some suggestions.

http://stackoverflow.com/questions/1646 ... ecycle-bin

mingolito
Posts: 28
Joined: 04 Dec 2014 11:34

Re: Remove Duplicate Files and Move them in the Bin.

#6 Post by mingolito » 17 Jun 2015 06:58

I tried to replace so:

Code: Select all

IF "%fsize%"=="%~z1" xcopy C:\$Recycle.bin "%~1"
but does not work... :(

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Remove Duplicate Files and Move them in the Bin.

#7 Post by foxidrive » 17 Jun 2015 07:06

mingolito wrote:I tried to replace so:

Code: Select all

IF "%fsize%"=="%~z1" xcopy C:\$Recycle.bin "%~1"
but does not work... :(


No, it won't. I'm not sure where you got that advice from as you didn't say.
FWIW you can't use xcopy in a way that the files will be added to the recycle bin - in the actual list of files.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Remove Duplicate Files and Move them in the Bin.

#8 Post by foxidrive » 17 Jun 2015 08:07

If you want to just move the files to a folder you create, that they can be retrieved from,
that can be done with plain batch code.

The copies stored that way should have a date-time added to the filename, at the beginning,
so you can see which are the earliest deleted files and which are the most recent deleted files,
and all that can be done easily.

mingolito
Posts: 28
Joined: 04 Dec 2014 11:34

Re: Remove Duplicate Files and Move them in the Bin.

#9 Post by mingolito » 17 Jun 2015 09:00

foxidrive wrote:If you want to just move the files to a folder you create, that they can be retrieved from,
that can be done with plain batch code.

The copies stored that way should have a date-time added to the filename, at the beginning,
so you can see which are the earliest deleted files and which are the most recent deleted files,
and all that can be done easily.


:) it's OK to just move the files to a folder you created, so as to recover the files if necessary :)
I know that you are a master, for you everything is easier, but I do not know how to do, I kindly ask help in changing the code. :? :|

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Remove Duplicate Files and Move them in the Bin.

#10 Post by foxidrive » 17 Jun 2015 19:51

mingolito wrote:
foxidrive wrote:If you want to just move the files to a folder you create, that they can be retrieved from,
that can be done with plain batch code.


:) it's OK to just move the files to a folder you created, so as to recover the files if necessary :)

I kindly ask help in changing the code. :? :|


Before I proceed in what you have asked, I need to know what the code you provided is supposed to be doing - because in my test here it was going to delete a single file (where there were two others of the same size, and more sets of duplicate files).

What is the task that is meant to be done by the batch file?

mingolito
Posts: 28
Joined: 04 Dec 2014 11:34

Re: Remove Duplicate Files and Move them in the Bin.

#11 Post by mingolito » 18 Jun 2015 07:20

foxidrive wrote:
mingolito wrote:
foxidrive wrote:If you want to just move the files to a folder you create, that they can be retrieved from,
that can be done with plain batch code.


:) it's OK to just move the files to a folder you created, so as to recover the files if necessary :)

I kindly ask help in changing the code. :? :|


Before I proceed in what you have asked, I need to know what the code you provided is supposed to be doing - because in my test here it was going to delete a single file (where there were two others of the same size, and more sets of duplicate files).

What is the task that is meant to be done by the batch file?


Simple code removes duplicate files in a folder, containing the same files in subfolders of the root folder.. :)

Example in a root folder called Pippo, inside there are 4 files + a subfolder with 4 files inside, 2 identical to those in the root folder
The code deletes the 2 files in the subfolder of the same name files the root folder.

The code I consider very good, to avoid duplication of files scattered in folders on your computer... :wink:

Logically I wanted for safety that the code moved the deleted files in a backup folder or the trash.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Remove Duplicate Files and Move them in the Bin.

#12 Post by foxidrive » 18 Jun 2015 19:10

Test this on some copies of your folders and files.

Line 2 is where the storage folder is set - don't place it in the same folder tree where you are executing the batch file.

Code: Select all

@echo off
set "storage_folder=d:\storage"
md "%storage_folder%" 2>nul

SET "StartFolder=."




FOR /r "%StartFolder%" %%i IN (*) DO call:doit "%%~i"
pause
GOTO:EOF

:doit

rem The four lines below will give you reliable YY DD MM YYYY HH Min Sec MS variables in XP Pro and higher.

for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%" & set "MS=%dt:~15,3%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%-%MS%"

SET "fname=%~nx1"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "fsize=!$%fname%!"
IF DEFINED $!fname! (
     ENDLOCAL
     IF "%fsize%"=="%~z1" (
          echo move "%~1" "%storage_folder%\%fullstamp%_%~nx1"
          move "%~1" "%storage_folder%\%fullstamp%_%~nx1" >nul
       )
) ELSE (
     ENDLOCAL
     SET "$%~nx1=%~z1"
)

GOTO:EOF

mingolito
Posts: 28
Joined: 04 Dec 2014 11:34

Re: Remove Duplicate Files and Move them in the Bin.

#13 Post by mingolito » 19 Jun 2015 02:40

woooo @foxidrive It is perfect bro....certainly an excellent application that may serve other users here in the forum... :)
One last thing, if you want to do it, to perfect the application, the possibility of including into backup folder that is created, the folder that contains the files duplicates.
Me explain better, if the duplicate files are in the folder A, into folder backup also insert the parent folder that contains the files duplicates.
in this mode I think it's absolutely perfect. :D :D
Thanks again for the time lost, you are a true Master.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Remove Duplicate Files and Move them in the Bin.

#14 Post by foxidrive » 19 Jun 2015 03:27

This works here in my simple tests.

I'm just clarifying that this tests for the same filename and filesize, but doesn't compare the contents of the files that are classified as duplicates.

Code: Select all

@echo off
set "storage_folder=d:\storage"
md "%storage_folder%" 2>nul

SET "StartFolder=."




FOR /r "%StartFolder%" %%i IN (*) DO call:doit "%%~i"
pause
GOTO:EOF

:doit

rem The four lines below will give you reliable YY DD MM YYYY HH Min Sec MS variables in XP Pro and higher.

for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%" & set "MS=%dt:~15,3%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%-%MS%"

SET "fname=%~nx1"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "fsize=!$%fname%!"
IF DEFINED $!fname! (
     ENDLOCAL
     IF "%fsize%"=="%~z1" (
          for %%p in ("%~dp1\.") do (
             md "%storage_folder%\%%~nxp" 2>nul
             echo move "%~1" "%storage_folder%\%%~nxp\%fullstamp%_%~nx1"
             move "%~1" "%storage_folder%\%%~nxp\%fullstamp%_%~nx1" >nul
          )
       )
) ELSE (
     ENDLOCAL
     SET "$%~nx1=%~z1"
)

GOTO:EOF

mingolito
Posts: 28
Joined: 04 Dec 2014 11:34

Re: Remove Duplicate Files and Move them in the Bin.

#15 Post by mingolito » 19 Jun 2015 04:10

foxidrive wrote:This works here in my simple tests.

I'm just clarifying that this tests for the same filename and filesize, but doesn't compare the contents of the files that are classified as duplicates.

Code: Select all

@echo off
set "storage_folder=d:\storage"
md "%storage_folder%" 2>nul

SET "StartFolder=."




FOR /r "%StartFolder%" %%i IN (*) DO call:doit "%%~i"
pause
GOTO:EOF

:doit

rem The four lines below will give you reliable YY DD MM YYYY HH Min Sec MS variables in XP Pro and higher.

for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%" & set "MS=%dt:~15,3%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%-%MS%"

SET "fname=%~nx1"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "fsize=!$%fname%!"
IF DEFINED $!fname! (
     ENDLOCAL
     IF "%fsize%"=="%~z1" (
          for %%p in ("%~dp1\.") do (
             md "%storage_folder%\%%~nxp" 2>nul
             echo move "%~1" "%storage_folder%\%%~nxp\%fullstamp%_%~nx1"
             move "%~1" "%storage_folder%\%%~nxp\%fullstamp%_%~nx1" >nul
          )
       )
) ELSE (
     ENDLOCAL
     SET "$%~nx1=%~z1"
)

GOTO:EOF


Thanks but I do not understand what you mean.
but doesn't compare the contents of the files that are classified as duplicates
:?:

Post Reply