S O L V E D Find path to sub folder and write to variable
Moderator: DosItHelp
S O L V E D Find path to sub folder and write to variable
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
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.
Re: Find path to sub folder and write to variable
Try something like this:…or: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!
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
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
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.
Re: Find path to sub folder and write to variable
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.
Re: Find path to sub folder and write to variable
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
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
Re: Find path to sub folder and write to variable
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.
Re: Find path to sub folder and write to variable
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%\"
Re: Find path to sub folder and write to variable
Fixed!foxidrive wrote:needs a backslash like so at the end "%systemdrive%\"kgeorge4 wrote:Do you think the solution suggested would be robust enough?
…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).
Re: Find path to sub folder and write to variable
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?
Re: Find path to sub folder and write to variable
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
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
Re: Find path to sub folder and write to variable
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.
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.
So once again, thank you very much for your input.
Keith
I had to remove the "" from the
Do Set "MyVar=%%A" so it was
Do Set MyVar=%%A, it then worked as described/expected.
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.
So once again, thank you very much for your input.
Keith
Re: Find path to sub folder and write to variable
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.
Hmm, that is very strange. I have never seen that cause any issues. You sure you were not missing a quote on either side?
Re: S O L V E D Find path to sub folder and write to vari
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.
It “seems” that “both” versions work equally.
I apologise to all concerned for the misinformation that I gave previously.
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.
It “seems” that “both” versions work equally.
I apologise to all concerned for the misinformation that I gave previously.
Re: S O L V E D Find path to sub folder and write to vari
Yes. Depending on how big your folder structure is, it could take some time to complete.