Solved

search for directory and set full path and directory as variable windows 7 and 10 batch

Posted on 2016-10-11
5
69 Views
Last Modified: 2016-10-14
I am using multiple systems all using windows 7 and windows 10 32bit and 64 bit
i want to write a batch to find a directory and set the directory as a variable
example
The dir i am looking for is "my directory"
in some systems the directory is in
c:\program files\directory\my directory
in other systems
C:\Program Files (x86)\directory\my directory
i want that the batch find the directory "my directory" and do a full copy to c:\backup

Thanks
0
Comment
Question by:Danny Kon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 84

Expert Comment

by:oBdA
ID: 41838029
Same as your other question; I don't see when this wouldn't work for your case.
If you see potential issues, please tell under which conditions this would not work:
@echo off
setlocal enabledelayedexpansion
set SourceList="C:\Program Files\directory\my directory" "C:\Program Files (x86)\directory\my directory"
set Target=C:\Backup
set Source=
for %%a in (%SourceList%) do (
	if exist "%%a" set Source=%%a
)
if not defined Source (
	echo No supported source folder found.
	exit /b 1
)
echo Source folder found: '%Source%'
robocopy.exe "%Source%" "%Target%" /e /L /r:0

Open in new window

0
 

Expert Comment

by:John Sam George
ID: 41838032
Test-path directory.
If it returns true, the directory exists
0
 
LVL 6

Author Comment

by:Danny Kon
ID: 41838159
oBdA

I did not a good job in explaining the problem sorry for that. The name of "my directory" can have 3 names
"My directory" "Mijn directorie" or "mijn folder"
Also the directory can be deeper in the system so users can set the directory to
C:\Program Files\directory\name of person\my directory
eg
C:\Program Files (x86)\directory\personal files\name\mijn folder

All the folders and all the files have to be copied to c:\backup

Thanks Danny
0
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 41838227
Try this then; as before, it's in test mode, remove the uppercase ECHO in line 13 to run it for real.
It assumes that the folders to copy are only found under one of "C:\Program Files\directory" or "C:\Program Files (x86)\directory", but not under both.
@echo off
setlocal enabledelayedexpansion
set SourceRoot="C:\Program Files\directory" "C:\Program Files (x86)\directory"
set FolderList="Mijn Folder" "Mijn directorie" "My directory"
set TargetRoot=C:\Backup
set ArgsFindstr=
for %%a in (%FolderList%) do set ArgsFindstr=!ArgsFindstr! /c:"\%%~a"
for %%a in (%SourceRoot%) do (
	for /f "delims=" %%d in ('dir /s /b /a:d "%%~a" 2^>NUL ^| findstr.exe /i /e %ArgsFindstr%') do (
		set Target=%%d
		set Target=%TargetRoot%!Target:%%~a=!
		echo '%%d' --^> '!Target!'
		ECHO robocopy.exe "%%d" "!Target!" /e /r:0
	)
)

Open in new window

0
 
LVL 6

Author Closing Comment

by:Danny Kon
ID: 41844061
I had to test this on different systems and it worked great

Thanks again for your quick and perfect help

Danny
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many software programs on offer that will claim to magically speed up your computer. The best advice I can give you is to avoid them like the plague, because they will often cause far more problems than they solve. Try some of these "do it…
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question