Jump to content
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble

MSFN is made available via donations, subscriptions and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, register and become a site sponsor/subscriber and ads will be disabled automatically. 


Sign in to follow this  
-X-

Need proper directory name variable for mklink batch file

Recommended Posts

Posted (edited)

Hey all. Long time. I'm trying to make a simple batch file that I can link to in SendTo that will take any folder and create a symbolic link in a specified folder. I'm just using %1 but it's returning the full path which does not work.

Here's what does not work because it translates to

 C:\>mklink /d C:\TargetFolder\C:\test C:\test

instead of

C:\>mklink /d C:\TargetFolder\test C:\test...

This is what I use...

mklink /d C:\Targetfolder\%1 %1

I think % number is probably totally wrong and I need one of this ~dp0 or whatever they are.

What's the actual variable I need to just return the directory name?

Edited by -X-
bad formatting

Share this post


Link to post
Share on other sites

You need to use variable expansion, see:

http://www.robvanderwoude.com/ntcall.php

%~n1 or %~nx1 should do.

Consider the possibility that the actual parameter contains a space (and thus needs to be wrapped in double quotes), i.e. it should be:

mklink /d "C:\Targetfolder\%~nx1" %1

%1 if needed should already contain the quotes, whilst the %~ strips them.

Also the above will get only the directory name, i.e. it will work for C:\test but not for C:\test\test2, as the %~nx1 will return only test2

jaclaz

Share this post


Link to post
Share on other sites
Posted (edited)

I had read similar pages to the ones you posted but since it states "expands %1 to a file name only" with no mention of directories, I thought it could only be used for files.

 

Your code worked even with folders that are levels deep for some reason.

 

Thanks.

Edited by -X-

Share this post


Link to post
Share on other sites
10 hours ago, -X- said:

I had read similar pages to the ones you posted but since it states "expands %1 to a file name only" with no mention of directories, I thought it could only be used for files.

Yes :), the definitions of the expansion are in some cases "wrong" (for simplicity) while being "right" :w00t:

It depends on the actual "type" of expansion used,  n and x only behave as "parsers" of the input, i.e. to be strict:

n actually means "whatever is after the rightmost backslash and before the rightmost dot (if any)"
x actually means "whatever is after the rightmost dot (if any)"

 

10 hours ago, -X- said:

Your code worked even with folders that are levels deep for some reason.

Define "it worked".

Of course "it worked", but "it worked" in a non-working way :w00t: (as I see it).

The %~nx1 expands to the file (or directory) name ONLY (Ok, ok, name + extension IF the extension is present).

Thus the result is "flattening" the link "tree".

I mean, if you have two original directories, like:

C:\mynicedir1\thisisadir

and

C:\mynicedir1\mynicedir2\thisisadir2

You will have as a result:

C:\Targetfolder\thisisadir

C:\Targetfolder\thisisadir2

which may (or may not) be what you want to obtain.

And what would happen if the originals are:

C:\mynicedir1\thisisadir

and

C:\mynicedir1\mynicedir2\thisisadir

:dubbio:

10 hours ago, -X- said:

Thanks.

You are welcome :).

jaclaz

Share this post


Link to post
Share on other sites

I do not know if this will help but here a VBS script that will list all the folders

in the parent folder directory. It will list each Path of The Folder and the Name 

of the Folder. It put all the info into a text file called ReportFolder.txt. It will 

then open the text file and once you close it ask to keep it or delete it.

Dim Act :Set Act = CreateObject("WScript.Shell")
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim Col, Obj, Ts 
Set Ts = Fso.CreateTextFile("ReportFolder.txt")
'-> Script In Same Folder You Want To Recursive
 Recursive(Fso.GetFolder("."))
'-> Path To Recursive Folder UnComment To Make Active
' Recursive(Fso.GetFolder("PLACE_PATH_TO_FOLDER"))
   Function Recursive(Folder)
    For Each Obj In Folder.SubFolders
     Col = Col & Folder.Path & vbCrLf & Folder.Name & vbCrLf 
     Recursive(Obj)
    Next
   End Function
'-> Make The Report With Info Collected 
  Ts.WriteLine Date() & " " & Time()
  Ts.WriteLine Col
  Ts.Close()
'-> Open Text File To read
  Act.Run("ReportFolder.txt"),1,True
'-> Ask if You Want To Keep Or Delete The Text File
 If MsgBox("Would you like to keep ReportFolder.txt,Yes to keep" & vbCrLf &_
 "No to delete",4132,"Keep Or Delete") = 6 Then 
   WScript.Quit()
 Else
  Fso.DeleteFile("ReportFolder.txt"),1 
 End If 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×