Solved

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

Posted on 2016-10-11
5
29 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
  • 2
  • 2
5 Comments
 
LVL 83

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 83

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

Why do Marketing keep bothering you?

Is your marketing department constantly asking for new email signature updates? Are they requesting a different design for every department? Do they need yet another banner added? Don’t let it get you down! There is an easy way to manage all of these requests...

Join & Write a Comment

Several part series to implement Internet Explorer 11 Enterprise Mode
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now