YYYYYAHHHHHH!!!
I got it to work. There were two things I needed to change in order to get this to work. I added a line "setLocal EnableDelayedExpansion" and I used "!" around the variables.
Here is what I changed:
@echo off
setLocal EnableDelayedExpansion
set list=
for /r H:\Docs %%g in (*.*) do (
set list=%%g
echo !list!
)
After I figured out how to fix this, I was able to remove the H:\Docs and replace it with the source path like this "%sourcePath%!list:%backPath%=!" (include quotes)
I created a batch file that does everything. It copies only files that have been changed (not every file). It then checks the %sourcePath% for any files that have been removed and deletes them from the backup. After the files are gone it removes empty directories.
If you like it, I could post it in the batch files section (if there is one).
Here is the full batch file:
@echo off
setLocal EnableDelayedExpansion
set backCMD=xcopy /c /f /d /e /h /i /r /y
set backPath=H:\Docs
set sourcePath=I:
echo Backing up %sourcePath%...
rem BACKUP THE FILES THAT HAVE BEEN CHANGED
%backCMD% %sourcePath% %backPath%
echo Removing old stuff in %backPath%...
rem CHECK FOR FILES THAT HAVE BEEN REMOVED FROM
rem THE SOURCE AND DELETE THEM FROM THE BACKUP DIRECTORY
for /r %backPath% %%g in (*.*) do (
set var=%%g
if not exist "%sourcePath%!var:%backPath%=!" (
echo File:!var!
del /f /q "!var!"
)
)
rem REMOVE ANY EMPTY DIRECTORIES FROM BACKUP
for /d /r %backPath% %%g in (*) do (
set var=%%g
if not exist "%sourcePath%!var:%backPath%=!" (
echo Dir:!var!
rd /s /q "!var!"
)
)
echo %date% : %time%>> backup.log
echo Finished backing up %sourcePath%
echo Log entry added in backup.log
pause
exit