S O L V E D Find path to sub folder and write to variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kgeorge4
Posts: 5
Joined: 08 Mar 2015 07:35

S O L V E D Find path to sub folder and write to variable

#1 Post by kgeorge4 » 08 Mar 2015 11:20

I have a batch file that uses xcopy to backup to external drive, this may change to robocopy later.

I would like to use this batch file on several computers, "BUT" each computer has a different path, to the sub folder that needs to be backed up, although, the folder that has to be backed up is “always” the same on “all” the computers.

So the question is, can the batch file with a shortcut on the desktop, look at drive “C” and find the sub folder called “Silver Dawn” and then store the path to the sub folder in a variable so that variable can be used in the xcopy command.

It is the sub folder called “Silver Dawn” and its sub folders and files that have to be backed up to the external drive.

If this can be done, I would also like it to be able to copy back “from” the external drive to the sub folder called “Silver Dawn” wherever it may be on the “C” drive of "any" of the computers. :|

TIA
Keith
Last edited by kgeorge4 on 26 Mar 2015 13:47, edited 1 time in total.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Find path to sub folder and write to variable

#2 Post by Compo » 08 Mar 2015 18:29

Try something like this:

Code: Select all

@Echo Off
SetLocal
Set "MyVar="
For /D /R "%SystemDrive%\" %%A In ("*Silver Dawn") Do Set "MyVar=%%A"
If Not Defined MyVar Exit/B
Echo(%%MyVar%%=%MyVar%
Pause
…or:

Code: Select all

@Echo Off
SetLocal
Set "MyVar="
For /F "Tokens=*" %%A In ('Dir/B/S/AD "%SystemDrive%\Silver Dawn" 2^>Nul') Do Set "MyVar=%%A"
If Not Defined MyVar Exit/B
Echo(%%MyVar%%=%MyVar%
Pause
It will of course work properly only if you have a single matching directory and I'm afraid I wouldn't trust a backup routine with something so hit and miss.

If you want something more robust you'd need to provide more detailed information!
Last edited by Compo on 12 Mar 2015 09:55, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find path to sub folder and write to variable

#3 Post by foxidrive » 09 Mar 2015 00:11

Compo wrote:If you want something more robust you'd need to provide more detailed information!


+1 The better details you provide, the better chance you'll get a script that works in your situation.

kgeorge4
Posts: 5
Joined: 08 Mar 2015 07:35

Re: Find path to sub folder and write to variable

#4 Post by kgeorge4 » 09 Mar 2015 01:42

Thank you both for your replies, I understand what you are saying about better info.

The only “for sure” thing I can give you is the folder name Silver Dawn, for example, on my computer the path is
C:\Users\KG\My Documents\Silver Dawn on another computer here it is
C:\Users\Home\Brenda\My Documents\Documents\Fishing\Silver Dawn on a third computer it is simply
C:\Silver Dawn so you can see what I am up against.

Do you think the solution suggested would be robust enough?

Keith

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Find path to sub folder and write to variable

#5 Post by Compo » 09 Mar 2015 12:53

Usually the path you require would be found in a specific place, i.e. registry OR config file (in a known location). A robust script would parse that information instead.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find path to sub folder and write to variable

#6 Post by foxidrive » 12 Mar 2015 03:14

kgeorge4 wrote:Do you think the solution suggested would be robust enough?


The second script of compo's should find the last folder in the drive named "silver dawn"

The upper script can also match a folder called "gold and silver dawn" for example and also needs a backslash like so at the end "%systemdrive%\"

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Find path to sub folder and write to variable

#7 Post by Compo » 12 Mar 2015 09:54

foxidrive wrote:
kgeorge4 wrote:Do you think the solution suggested would be robust enough?
needs a backslash like so at the end "%systemdrive%\"
Fixed!

…although I'd love to know why i've just tested "D:" and "D:\" which both act correctly but "C:" and "%SystemDrive%" do not match that of "C:\" and "%SystemDrive%\"; (even changing the working directory to the root of D: did not change that anomaly).

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find path to sub folder and write to variable

#8 Post by foxidrive » 13 Mar 2015 21:44

Compo wrote:but "C:" and "%SystemDrive%" do not match that of "C:\" and "%SystemDrive%\"; (even changing the working directory to the root of D: did not change that anomaly).


Did you change to the root of C: as well?

kgeorge4
Posts: 5
Joined: 08 Mar 2015 07:35

Re: Find path to sub folder and write to variable

#9 Post by kgeorge4 » 14 Mar 2015 12:33

Thanks for your answers Guys.

I am away until around the 26th March I will have a look at it all then, then I will come back to you.

Tahnks

Keith :oops:

kgeorge4
Posts: 5
Joined: 08 Mar 2015 07:35

Re: Find path to sub folder and write to variable

#10 Post by kgeorge4 » 26 Mar 2015 13:39

OK guys, thank you for the above.

I had to remove the "" from the

Do Set "MyVar=%%A" so it was
Do Set MyVar=%%A, it then worked as described/expected. :D

I have used
robocopy "%MyVar%" "F:\Backed up Files"\ /e /mir /log:backup_log.txt
and again I got the results that I require on the (4) machines I have tried it on so far. :D

So once again, thank you very much for your input.

Keith

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Find path to sub folder and write to variable

#11 Post by Squashman » 26 Mar 2015 15:32

kgeorge4 wrote:I had to remove the "" from the

Do Set "MyVar=%%A" so it was
Do Set MyVar=%%A, it then worked as described/expected. :D

Hmm, that is very strange. I have never seen that cause any issues. You sure you were not missing a quote on either side?

kgeorge4
Posts: 5
Joined: 08 Mar 2015 07:35

Re: S O L V E D Find path to sub folder and write to vari

#12 Post by kgeorge4 » 27 Mar 2015 12:56

Hi Squashman,
I copied and pasted the code from the second post. When I ran the code, it seemed to freeze the computer so I ended the process.
As an experiment I removed the quotes from Do Set "MyVar=%%A" I then saved and ran the batch file again, I got distracted and when I looked back, the batch file had completed, successfully.

After I read your post, I went back to the original code, I then noticed that this also worked correctly. The original fault was me not waiting long enough for the batch file to complete the copy process. :oops:

It “seems” that “both” versions work equally.

I apologise to all concerned for the misinformation that I gave previously.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: S O L V E D Find path to sub folder and write to vari

#13 Post by Squashman » 27 Mar 2015 13:10

Yes. Depending on how big your folder structure is, it could take some time to complete.

Post Reply