[SOLVED] CMD script to create multiple directory junctions using mklink

WebMaximus

Contributor
Joined
Sep 26, 2018
Posts
32
I guess my question isn't really about programming in its true word but more about scripting and how to create a cmd script to create multiple directory junctions using the mklink command.

Here's what I'm trying to achieve:

I have a Temp folder (F:\Temp) containing 736 subfolders called zOrtho4XP_+34+024, zOrtho4XP_+34+025, zOrtho4XP_+34+026 etc ranging all the way to zOrtho4XP_+061+027.

In each of these zOrtho4XP subfolders, I want to create a directory junction called textures that should point to the same zOrtho4XP\textures subfolder located on another disk where the path is E:\Ortho4XP\Tiles\zOrtho4XP_+34+024\textures, E:\Ortho4XP\Tiles\zOrtho4XP_+34+025\textures etc...

I've used the mklink command manually a couple of times to test as seen here:

mklink /H /J F:\Temp\zOrtho4XP_+34+024\textures E:\Ortho4XP\Tiles\zOrtho4XP_+34+024\textures
mklink /H /J F:\Temp\zOrtho4XP_+34+025\textures E:\Ortho4XP\Tiles\zOrtho4XP_+34+025\textures
...

Question is how to automate/script this to avoid having to run this command manually 700+ times and changing the numbers each and every time?

Many thanks in advance for any ideas how and if this can be done!
 
See: While loop in batch for information on running the equivalent of a while loop that lets you increment a counter. You may need to nest one loop inside another. Read through that whole article and all the examples. One of them should be exactly what you're looking for.

I'm not sure why you'd want a hard link rather than just a straight symbolic link. The /J switch should get you what you're looking for, I believe. Refer to the myriad tutorials and other documentation for the mklink command to decide what you actually need.
 
Thanks for the suggestion! I had a quick look and found some interesting examples.

What I think makes my situation a bit trickier is the numbers in the names of the folders are not in a perfect order. Instead, they look as seen below. Which makes me think I somehow need to 'fetch' the name of the folders. And then use the fetched information as input when running the command to create each directory junction.

So, a bit like this -> Get the name of the first subfolder in F:\Temp. Then use this name to know in what subfolder in the target location to create the directory junction.

I know of the site you linked to and guess it's more suitable for these kind of questions. So will head over there and post the same question and see if someone will be able to help me. Would love to being able to figure this one out myself. Unfortunately though, I feel this is a bit over my head. Which is why I'm looking for help from the real experts in this area.

Thanks anyway!

Greenshot 2020-01-16 19_08_36-Tiles.jpg
 
You will get a lot of good information with regard to scripting at stackoverflow.

Based on what you've said above, if the "destination" names are directly derived from the source names, then you'll probably end up using a FOR loop where you have the entire list of source folders given in the FOR itself, then use string parsing commands for each single source to construct the destination name and run the mklink command. See: FOR loop in Windows for an example of what I'm discussing.
 
Yep, something like that is what I'm thinking too. Just need to figure out how to transform my thoughts into a working script :-)

Hopefully someone over at stackoverflow will be able to help me out.

Thanks again for pointing me in the right direction!
 
Wonderful! And thank you very much for posting a direct link to the reference material that resulted in your success. Future searchers will thank you, too.
 
NP, I know from own experience how happy you are those times you find an already existing solution to a problem you're having. Which is the reason I always try to remember sharing these kind of things.
 
Hi,

Sorry only notice this post today...

You don't need to use an accessory file because the for command can get the list of folders using /D
Code:
cd /d F:\Temp
for /D %d in (*.*) do mklink /H /J F:\Temp\%d\textures E:\Ortho4XP\Tiles\%d\textures

Your dir cmd will include also file names if they exist inside the F:\Temp folder. To get a list of only folders you need to use dir /b /AD
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top