Move files with Robocopy

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
barnabe0057
Posts: 21
Joined: 04 Aug 2017 14:20
Location: France

Move files with Robocopy

#1 Post by barnabe0057 » 09 Apr 2023 15:29

Hi guys,
I'm trying to create a script that sorts files in bulk according to their extension. I use robocopy to move the files.
The script is functional, however I encounter a use case where robocopy copies a file but does not delete it from the source. This happens when the tree to be processed contains duplicate files, robocopy will delete the first file encountered but not the duplicate files.
Does anyone have a workaround? Thank you.

This is the script :

Code: Select all

@echo off
setlocal enableextensions enabledelayedexpansion
ver | find "10.0." >nul && chcp 65001 >nul || chcp 28591 >nul
color 0A

:: on définit le dossier racine à trier
:: set "targetDirectory=%USERPROFILE%\Downloads"
set "targetDirectory="

if defined targetDirectory (cd /d %targetDirectory% || exit /b 1) else (set "targetDirectory=%CD%")

:: on définit le répertoire de destination pour chaque type de fichier
set "homeDirectory=%HOMEDRIVE%%HOMEPATH%\Test"

set "ProgrammesPath=%homeDirectory%\Downloads"
set "VideosPath=%homeDirectory%\Videos"
set "ImagesPath=%homeDirectory%\Pictures"
set "MusiquesPath=%homeDirectory%\Music"
set "DocumentsPath=%homeDirectory%\Documents"
set "TorrentsPath=%homeDirectory%\Downloads\Torrents"

:: on définit les extensions par catégorie de fichiers
set "ProgrammesExt=.exe .msi .iso .bin"
set "VideosExt=.mkv .avi .mp4 .mpg .mpeg .mov .wmv .flv"
set "ImagesExt=.jpeg .jpg .png .tiff .gif"
set "MusiquesExt=.mp3 .mid .flac .m3u .wma .aac .wav .ape"
set "DocumentsExt=.doc .docx .xls .xlsx .pptx .xlsm .docm .ppt .odt .ods .odp .odb .odg .rtf .txt .pdf"
set "TorrentsExt=.torrent .metalink"

:: on affiche les dossiers de rangement
call :display1

:: on traite les sous-dossiers
call :subdirectories

:: on traite les fichiers du dossier racine
call :files "%targetDirectory%"

echo. & pause
call :display2

timeout /t 3
exit /b 0

:subdirectories
for /f "tokens=*" %%A in ('dir /b /s /ad') do call :files "%%~A"
goto :eof

:files
for %%A in (Documents Musiques Videos Images Programmes Torrents) do (
	for %%B in (!%%AExt!) do (
		if exist "%~1\*%%B" (
			robocopy "%~1" "!%%APath!" "*%%B" /MOVE /NJH /NJS
		)
	)
)
goto :eof

:display1
echo.
echo *****************************************************************************
echo.
echo      REPERTOIRE A RANGER = "%targetDirectory%"
echo.
echo *****************************************************************************
echo.
echo.
echo Programmes = "%ProgrammesPath%"
echo.
echo Videos     = "%VideosPath%"
echo.
echo Images     = "%ImagesPath%"
echo.
echo Musiques   = "%MusiquesPath%"
echo.
echo Documents  = "%DocumentsPath%"
echo.
echo Torrents   = "%TorrentsPath%"
echo.
echo.

timeout /t 5
cls
echo.
goto :eof

:display2
cls
color 0A
echo.
echo ***********************************************
echo.
echo *****   FIN DE LA SEQUENCE DE RANGEMENT   *****
echo.
echo ***********************************************
echo.
echo.
goto :eof
edit1 : even by restarting the script a second time, the duplicate files are not erased, this means that Robocopy does not process the file if it already exists in the destination. How to change this behavior?

barnabe0057
Posts: 21
Joined: 04 Aug 2017 14:20
Location: France

Re: Move files with Robocopy

#2 Post by barnabe0057 » 09 Apr 2023 16:29

Problem solved !
To force the processing of duplicate files I added the switches /IM /IS /IT

Post Reply