Solved

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

Posted on 2016-10-11
5
33 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 won’t your email signature format correctly?

Struggling to get your corporate email signatures to format correctly? Does the logo keep resizing? Is the text appearing too big? What can you do to prevent this? Find out how you can save your signatures today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
login windows 10 4 42
How to do advance search in Windows 2008? 11 37
Can't get access/ownership to folder 3 88
Windows Server 2003 Policy Preventing Updates 6 18
A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

910 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

28 Experts available now in Live!

Get 1:1 Help Now