1.

Solve : sync folders?

Answer»

Hi, this is my first post here, so please forgive me if I have done something wrong (posted in the wrong place or i dunno)

for /r "%~d0\Music\" %%F in (*) do if not exist "C:\Users\Polle\Music\%%~nxF" del "%~d0\Music\%%~nxF"

So basically wath I'm trying to do is this: I'm trying to use this for command to find all files on my hard drive that are no longer on my computer and delete those files from my harddrive

Now, wath this command is doing:
-It works correct in the folder "%~d0\Music"
-It does nothing in the folder "%~d0\Music\someartisthere\"

example: it checks my HARDRIVE and finds the file "%~d0\Music\ACDC\thunder.mp3"
now on my pc it should look for "C:\Users\Polle\Music\ACDC\thunder.mp3"
but it is looking for "C:\Users\Polle\Music\thunder.mp3" which does not exist
so it will try to delete "%~d0\Music\thunder.mp3" wich does not exist either

nothing happens

Can anyone help me out on this one? More INFO, I gues I didn't make myself clear:
I have an external HDD, and I want to make sure that if a file is no longer on my computer it is no longer on my external HDD either, I'm basicly trying to syncronize my external drive with my computer

heres the full script (it's mainly in dutch tough)


Code: [Select]echo off

title = Folder Backup!  (program by Polle)
color 0e
MKDIR %~d0\Muziek
MKDIR %~d0\willekeurig
MKDIR %~d0\games
MKDIR %~d0\Vuze
MKDIR %~d0\batch
MKDIR %~d0\batch\special
MKDIR %~d0\restore\registry
cls





set /P backup= Backup van alles maken? (j/n) :
if %backup% EQU j goto B

set /P muziek= Backup van muziek maken? (j/n) :
set /P willekeurig= Backup van documenten maken? (j/n) :
set /P GAMES= Backup van games maken? (j/n) :
set /P Vuze= Backup van Vuze maken? (j/n) :
set /P Batch= Backup van batch bestanden maken? (j/n) :
set /P Registry= Backup van registry maken? (j/n) :
goto A




:A


set start1=%time:~0,2%
set start2=%time:~3,2%
set start3=%time:~6,2%
set starttime=%start1%:%start2%:%start3%


title = making music backup... (program by Polle)
if %muziek% EQU j xcopy /d /c /y "C:\Users\Polle\Music" "%~d0\Muziek"
for /r "%~d0\Muziek\" %%F in (*.*) do if not exist "C:\Users\Polle\Music\%%~nxF" del "%~d0\Muziek\%%~nxF"

title = making documents backup... (program by Polle)
if %willekeurig% EQU j xcopy /s /d /c /y "C:\Users\Polle\willekeurig" "%~d0\willekeurig\"
for /r "%~d0\willekeurig\" %%F in (*.*) do if not exist "C:\Users\Polle\willekeurig\%%~nxF" del "%~d0\willekeurig\%%~nxF"

REM title = making games backup... (program by Polle)
REM if %GAMES% EQU j xcopy /s /d /c /y "C:\Users\Polle\NDS games" "%~d0\games\NDS\"
REM if %GAMES% EQU j xcopy /s /d /c /y "C:\Users\Public\Games\World of Warcraft\instal" "%~d0\games\WoW\"
REM for /r "%~d0\games\NDS\" %%F in (*.*) do if not exist "C:\Users\Polle\NDS games\%%~nxF" del "%~d0\games\NDS\%%~nxF"
REM for /r "%~d0\games\WoW\" %%F in (*.*) do if not exist "C:\Users\Public\Games\World of Warcraft\instal\%%~nxF" del "%~d0\games\WoW\%%~nxF"

title = making Vuze backup... (program by Polle)
if %Vuze% EQU j xcopy /s /d /c /y "C:\Users\Polle\Vuze" "%~d0\Vuze\"
for /r "%~d0\Vuze\" %%F in (*.*) do if not exist "C:\Users\Polle\Vuze\%%~nxF" del "%~d0\Vuze\%%~nxF"

title = making batch backup... (program by Polle)
if %Batch% EQU j xcopy /s /d /c /y "C:\Users\Polle\Desktop\danger" "%~d0\batch\"
if %Batch% EQU j xcopy /s /d /c /y /h "C:\Program Files\CwindowsMD" "%~d0\batch\special\"
for /r "%~d0\batch\" %%F in (*.*) do if not exist "C:\Users\Polle\Desktop\danger\%%~nxF" del "%~d0\batch\%%~nxF"
for /r "%~d0\batch\special\" %%F in (*.*) do if not exist "C:\Program Files\CwindowsMD\%%~nxF" del "%~d0\batch\special\%%~nxF"


REM title = making registry backup... (program by Polle)
REM if %Registry% EQU j xcopy /s /d /c /y "C:\regbackup" "%~d0\restore\registry\"
REM for /r "%~d0\restore\registry\" %%F in (*.*) do if not exist "C:\regbackup\%%~nxF" del "%~d0\restore\registry\%%~nxF"


set stop1=%time:~0,2%
set stop2=%time:~3,2%
set stop3=%time:~6,2%
set stoptime=%stop1%:%stop2%:%stop3%

set /A result1=%stop1% - %start1%
set /A RESULT2=%stop2% - %start2%
set /A result3=%stop3% - %start3%
set resulttime=%result1%:%result2%:%result3%

cls
echo start = %starttime%
echo stop = %stoptime%
echo duration = %resulttime%
pause


exit






:B
set muziek=j
set willekeurig=j
set GAMES=j
set Vuze=j
set Batch=j
set Registry=j
goto A



The non-functional parts are:

for /r "%~d0\Muziek\" %%F in (*.*) do if not exist "C:\Users\Polle\Music\%%~nxF" del "%~d0\Muziek\%%~nxF"
for /r "%~d0\willekeurig\" %%F in (*.*) do if not exist "C:\Users\Polle\willekeurig\%%~nxF" del "%~d0\willekeurig\%%~nxF"
for /r "%~d0\Vuze\" %%F in (*.*) do if not exist "C:\Users\Polle\Vuze\%%~nxF" del "%~d0\Vuze\%%~nxF"
for /r "%~d0\batch\" %%F in (*.*) do if not exist "C:\Users\Polle\Desktop\danger\%%~nxF" del "%~d0\batch\%%~nxF"
for /r "%~d0\batch\special\" %%F in (*.*) do if not exist "C:\Program Files\CwindowsMD\%%~nxF" del "%~d0\batch\special\%%~nxF"

It fails in the subdirectories of these foldersI 'm desperate! PLEASE, i BEG of you, can't anyone help me?

I FOUND an old post of someone that has a similar problem, he didn't find a solution either:

Quote

Right now I've got a batch file to backup from My Documents to a NETWORKED drive at work. But I was wondering if there was a way to do this, but check to see if a file has been deleted. I have gotten rid of some files in my documents and they are still on my network drive. If I don't delete them from both at the same time I forget which ones they are.
Anyway, I know this is possible with a 3rd party software, but I wanted to get it done throught his batch file if at all possible, since it is a work computer and I'm not supposed to install anything onto it
Ok, It took me over 5 days, but I finally found a solution, well, a partial solution anyway, heres the new code for anyone that's interested:

Code: [Select]echo off

title = Folder Backup!  (program by Polle)
color 0e
MKDIR %~d0\Music
MKDIR %~d0\willekeurig
MKDIR %~d0\Vuze
MKDIR %~d0\batch
MKDIR %~d0\CwindowsMD
cls





set /P backup= Backup van alles maken? (j/n) :
if %backup% EQU j goto B

set /P muziek= Backup van muziek maken? (j/n) :
set /P willekeurig= Backup van documenten maken? (j/n) :
set /P Vuze= Backup van Vuze maken? (j/n) :
set /P Batch= Backup van batch bestanden maken? (j/n) :
goto A




:A


set start1=%time:~0,2%
set start2=%time:~3,2%
set start3=%time:~6,2%
set starttime=%start1%:%start2%:%start3%


title = making music backup... (program by Polle)
if %muziek% EQU j xcopy /d /c /y "C:\Users\Polle\Music" "%~d0\Music"
for /r "%~d0\Music\" %%F in (*.*) do echo %%F>> file.txt
for /F "tokens=2* delims=\" %%I in (file.txt) do if not exist "C:\Users\Polle\%%I\%%J" del "%~d0\%%I\%%J"
del file.txt



title = making documents backup... (program by Polle)
if %willekeurig% EQU j xcopy /s /d /c /y "C:\Users\Polle\willekeurig" "%~d0\willekeurig\"
for /r "%~d0\willekeurig\" %%F in (*.*) do echo %%F>> file.txt
for /F "tokens=2* delims=\" %%I in (file.txt) do if not exist "C:\Users\Polle\%%I\%%J" del "%~d0\%%I\%%J"
del file.txt



title = making Vuze backup... (program by Polle)
if %Vuze% EQU j xcopy /s /d /c /y "C:\Users\Polle\Vuze" "%~d0\Vuze\"
for /r "%~d0\Vuze\" %%F in (*.*) do echo %%F>> file.txt
for /F "tokens=2* delims=\" %%I in (file.txt) do if not exist "C:\Users\Polle\%%I\%%J" del "%~d0\%%I\%%J"
del file.txt


title = making batch backup... (program by Polle)
if %Batch% EQU j xcopy /s /d /c /y "C:\Users\Polle\Desktop\danger" "%~d0\batch\"
if %Batch% EQU j xcopy /s /d /c /y /h "C:\Program Files\CwindowsMD" "%~d0\CwindowsMD"
for /r "%~d0\batch\" %%F in (*.*) do echo %%F>> file.txt
for /F "tokens=2* delims=\" %%I in (file.txt) do if not exist "C:\Users\Polle\Desktop\%%I\%%J" del "%~d0\%%I\%%J"
del file.txt
for /r "%~d0\CwindowsMD\" %%F in (*.*) do echo %%F>> file.txt
for /F "tokens=2* delims=\" %%I in (file.txt) do if not exist "C:\Program Files\%%I\%%J" del "%~d0\%%I\%%J"
del file.txt



set stop1=%time:~0,2%
set stop2=%time:~3,2%
set stop3=%time:~6,2%
set stoptime=%stop1%:%stop2%:%stop3%

set /A result1=%stop1% - %start1%
set /A result2=%stop2% - %start2%
set /A result3=%stop3% - %start3%
set resulttime=%result1%:%result2%:%result3%

cls
echo start = %starttime%
echo stop = %stoptime%
echo duration = %resulttime%
pause


exit






:B
set muziek=j
set willekeurig=j
set Vuze=j
set Batch=j
goto A


It now finds and deletes 99% of the incorrect files, but it doesn't delete the incorrect folders

If anyone knows a similar solution to delete the folders, please tell, but for now, this is good enough for me

* NOTE: I did clean up the file a bit tough, so it's smaller, so it doesn't perfectly reflect the first file i posted *FINAL SOLUTION:
this removes files AND directories

This finds all files that are on your external HDD but not on your home pc and erases them:
note: I am comparing the folders "%~d0\Music\" (on my HDD) and "C:\Users\Polle\Music" (on my home PC)

Code: [Select]for /r "%~d0\Music\" %%F in (*.*) do echo %%F>> file.txt
for /F "tokens=2* delims=\" %%I in (file.txt) do if not exist "C:\Users\Polle\%%I\%%J" del "%~d0\%%I\%%J"
del file.txt

this will delete all remaining empty folders on my HDD:

Code: [Select]for /f "usebackq delims=" %%d in (`"dir /ad/b/s | sort /R"`) do rd "%%d"


Discussion

No Comment Found