0

If I have a path C:\folder1\folder2\folder3\keyfolder\folder4\file.ext and a string keyfolder which corresponds to one of the subfolders in the path, how can I change the path root up until the keyfolder string to have a result such as C:\newfolder1\newfolder2\keyfolder\folder4\file.ext? Basically what I am trying to do is change the folder structure of a file up until a keyfolder folder.

SET OLD=C:\folder1\folder2\folder3\keyfolder\folder4\file.ext
SET KEY=keyfolder
SET NEW=C:\newfolder1\newfolder2

Now I just need to replace OLD with NEW up until KEY.

Thank you!

Andrew

Andrew
  • 293
  • 6
  • 14
  • Do you know how to search for a string? What have you tried? – Floris Feb 12 '13 at 13:21
  • See http://stackoverflow.com/questions/7005951/batch-file-find-if-substring-is-in-string-not-in-a-file – Floris Feb 12 '13 at 13:22
  • @Floris that linked helped loads and I managed to figure it out, thanks! – Andrew Feb 12 '13 at 14:02
  • Great! Why don't you add what you discovered as the answer - it will help other people that have a similar problem, in the future. It's how we all help SO to be the best it can be... – Floris Feb 12 '13 at 14:15

1 Answers1

1

This will work as long as the keyfolder name does not contain =.

@echo off
setlocal enableDelayedExpansion
set "OLD=C:\folder1\folder2\folder3\keyfolder\folder4\file.ext"
set "KEY=keyfolder"
set "NEW=C:\newfolder1\newfolder2"

set "MOD=%NEW%\%KEY%\!OLD:*\%KEY%\=!"
echo MOD=!MOD!
dbenham
  • 127,446
  • 28
  • 251
  • 390