Solved

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

Posted on 2016-10-11
5
78 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 85

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 85

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Determining the an SCCM package name from the Package ID
Configuring Remote Assistance for use with SCCM
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

707 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