Remove variable string from another variable

May 27, 2010 at 11:05:43
Specs: Windows XP/Vista/7
So this is basically what I'm trying to do,
remove the string stored in %DST% from the
string %fullname%.
set DST=c:\test\laptop
set relname=!fullname:!DST!=!

If it is not possible to do this, then let me
explain why I'm trying to, and maybe it can be
approached from a different angle...

I need to compare all the files in a destination
directory (and subdirectories) with those of a
source directory. If a file in the destination
does not exist in the source, delete it. Within
their respective root directories (%SRC% and
%DST%), the directory structures are
identical. I would like to keep the source and
destination root directories as variables so any
necessary changes will only have to be made
in one place in the file.

This is what I currently have. It works only if I
type the literal DST string in the following:
set relname=!fullname:!DST!=!
I appreciate any help!

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set SRC=\\fox-server\data
set DST=c:\test\laptop

For /f "delims=" %%a in ('Dir /s /b %DST%')
do (
set fullname=%%a
set relname=!fullname:!DST!=!

if not exist "!SRC!\!relname!" (
echo !DST!\!relname!
rmdir "!DST!\!relname!" /s /q
del "!DST!\!relname!" /q
)
)

pause


See More: Remove variable string from another variable

Report •

#1
May 27, 2010 at 12:25:36
@echo off & setLocal EnableDELAYedeXpansion

set fullname=c:\test\laptop\bla
set DST=c:\test\laptop
set relname=!fullname:%DST%=!
echo.!relname!


=====================================
Helping others achieve escape felicity

M2


Report •

#2
May 27, 2010 at 12:34:15
since DST is assigned outside (prior to) the for-loop, you should be able to get away with:
set relname=!fullname:%DST%=!
another way:
for /f "tokens=1-3* delims=/" %%a in ('dir /b /s...
set dst=%%a/%%b/%%c/%%d
...
but that locks it in at third-level directory on the source.

Report •

#3
May 27, 2010 at 12:46:01
To separate them out, try this:

=============================
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b/s/a-d') do (
echo.%%~DPa
echo.%%~NXa
)


=====================================
Helping others achieve escape felicity

M2


Report •
Related Solutions


Ask Question