Page 1 of 2
Remove Duplicate Files and Move them in the Bin.
Posted: 16 Jun 2015 08:31
by mingolito
Mod Edit: This doesn't compare the contents of filesHello 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.
Re: Remove Duplicate Files and Move them in the Bin.
Posted: 16 Jun 2015 14:55
by foxidrive
Third party tools exist that will delete to the recycle bin, rather than using the DEL command.
Re: Remove Duplicate Files and Move them in the Bin.
Posted: 17 Jun 2015 04:37
by mingolito
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 ???
Re: Remove Duplicate Files and Move them in the Bin.
Posted: 17 Jun 2015 05:56
by ShadowThief
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.
Re: Remove Duplicate Files and Move them in the Bin.
Posted: 17 Jun 2015 06:41
by foxidrive
Re: Remove Duplicate Files and Move them in the Bin.
Posted: 17 Jun 2015 06:58
by mingolito
I tried to replace so:
Code: Select all
IF "%fsize%"=="%~z1" xcopy C:\$Recycle.bin "%~1"
but does not work...

Re: Remove Duplicate Files and Move them in the Bin.
Posted: 17 Jun 2015 07:06
by foxidrive
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.
Re: Remove Duplicate Files and Move them in the Bin.
Posted: 17 Jun 2015 08:07
by foxidrive
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.
Re: Remove Duplicate Files and Move them in the Bin.
Posted: 17 Jun 2015 09:00
by mingolito
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.

Re: Remove Duplicate Files and Move them in the Bin.
Posted: 17 Jun 2015 19:51
by foxidrive
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?
Re: Remove Duplicate Files and Move them in the Bin.
Posted: 18 Jun 2015 07:20
by mingolito
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...
Logically I wanted for safety that the code moved the deleted files in a backup folder or the trash.
Re: Remove Duplicate Files and Move them in the Bin.
Posted: 18 Jun 2015 19:10
by foxidrive
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
Re: Remove Duplicate Files and Move them in the Bin.
Posted: 19 Jun 2015 02:40
by mingolito
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.
Thanks again for the time lost, you are a true Master.
Re: Remove Duplicate Files and Move them in the Bin.
Posted: 19 Jun 2015 03:27
by foxidrive
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
Re: Remove Duplicate Files and Move them in the Bin.
Posted: 19 Jun 2015 04:10
by mingolito
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
