Need help with backup batch

Home / HOME
January 28, 2009 at 02:48:27
Specs: Win XP sp3, 2.3 / 1gb
Please don't be too harsh as I am very new to batch.

I xcopy using /d switch.
I am trying to delete files that are not needed any more. eg have been removed from D:\MyDir\Perhaps sometimes sub directory
I want to look into E:\MyDir\Perhaps sometimes sub directory
Then look into the corresponding D:\MyDir\Perhaps sometimes sub directory
and see if they exist if not then move them for deletion

dir /b "D:\*******\" | find "" /v >D:\Dcount.txt
for /f "tokens=*" %%b in (D:\Dcount.txt) do (
set var=%%b
dir /b "E:\*******\" | find "" /v >D:\Ecount.txt
for /f "tokens=*" %%c in (D:\Ecount.txt) do (
set var1=%%c
pushd "E:\*******\%var1%"
for %%m in (*) do if not exist "D:\*******\%var%\%%m" do (
move "%%m" D:\TempRecycle
)
)
)
Error is The system cannot find the file specified.

I can do this

pushd E:\?????
for %%k in (*) do if not exist "D:\????\%%k" move "%%k" D:\TempRecycle
popd
pushd E:\???\amr
for %%k in (*) do if not exist "D:\????\amr\%%k" move "%%k" D:\TempRecycle
popd

But if I delete any folders but forget to change the batch it errors and shoots into windows and moves loads of stuff for deletion. TempRecycle is on CCleaners list to be wiped.


See More: Need help with backup batch

Report •


#1
January 28, 2009 at 03:31:49
Your whole batch is higly questionable, but first of all it never works as you refer a variable %var% and %var1% defined inside the same For loop: that is not allowed.

You have to enable the delayed expansion by coding

@echo off & setlocal EnableDelayedExpansion

then use the ! to mark variables instead of the conventional %, i.e. !var! and !var1!.

About wildcards you can use multiple ? but just one * and they are not allowed in pushd or cd statements where they are of no meaning.

In my opinion you are trying harder than your actual scripting know-how.


Report •

#2
January 28, 2009 at 04:02:15
sorry IVO to mislead you I am not using multiple wildcards I put them in to symbalise a directory. Obviously this is beyond my scripting know-how or I would not be bothering asking for help.
here is a slightly more representative example
@echo off & setlocal enabledelayedexpansion
cls
dir /b "D:\MyDIR\" | find "" /v >D:\Dcount.txt
for /f "tokens=*" %%b in (D:\Dcount.txt) do (
set var=%%b
dir /b "E:\MyDIR\" | find "" /v >D:\Ecount.txt
for /f "tokens=*" %%c in (D:\Ecount.txt) do (
set var1=%%c
pushd "E:\MyDIR\!var1!"
for %%m in (*) do if not exist "D:\MyDIR\!var!\%%m" move "%%m" D:\TempRecycle
)
)
popd
pause
exit
This now moves everything in all directories and sub directories from E:\MyDIR

Report •

Related Solutions


Ask Question