Solved

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

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

Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
powershell script 9 65
Disk Cleanup and Temporary Files 15 57
Mask & unmask data in journal 6 21
batch file or script 4 27
When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
How to record audio from input sources to your PC – connected devices, connected preamp to record vinyl discs, streaming media, that play through your audio card: Vista, Windows 7, Windows 8, Windows 8.1 and Windows 10 – both 32 bit & 64.
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 Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

828 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