Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a batch file discover if a directory is actually a Junction?

I'm writing a Batch file (.bat) and I couldn't find a way to discover if a given directory I have the path to is a real directory or a Junction (created on Windows 7 by using mklink /j). Can anyone point me in the right direction?

like image 944
ArmlessJohn Avatar asked Jan 30 '11 19:01

ArmlessJohn


People also ask

What does && do in batch file?

&& runs the second command on the line when the first command comes back successfully (i.e. errorlevel == 0 ). The opposite of && is || , which runs the second command when the first command is unsuccessful (i.e. errorlevel != 0 ).

What does * mean in batch file?

The %* modifier is a unique modifier that represents all arguments passed in a batch file. You cannot use this modifier in combination with the %~ modifier. The %~ syntax must be terminated by a valid argument value. Source: "Using batch parameters" on Microsoft.com (defunct)

How do you check if a directory exists in Windows?

The Test-Path Cmdlet $Folder = 'C:\Windows' "Test to see if folder [$Folder] exists" if (Test-Path -Path $Folder) { "Path exists!" } else { "Path doesn't exist." } This is similar to the -d $filepath operator for IF statements in Bash. True is returned if $filepath exists, otherwise False is returned.


2 Answers

In a batch script you can use the following:

 SET Z=&& FOR %%A IN (linkfilename) DO SET Z=%%~aA
 IF "%Z:~8,1%" == "l" GOTO :IT_A_LINK

this is quicker than calling DIR /AL.

The %%~aA gets the attributes of the "linkfilename",
a 9 char string like d-------- (a directory),
or d-------l a link to a directory,
or --------l a link to a file.

%Z:~8,1% then grabs just the reparse point attribute.

like image 115
jeffd Avatar answered Nov 02 '22 23:11

jeffd


I have this little gem which will list all Junctions and their targets in your current directory:

for /F "delims=;" %j in ('dir /al /b') do @for /F "delims=[] tokens=2" %t in ('dir /a ^| findstr /C:"%j"') do @echo %j :: %t

Example output:

Application Data :: C:\Users\AB029076\AppData\Roaming
Cookies :: C:\Users\AB029076\AppData\Roaming\Microsoft\Windows\Cookies
Local Settings :: C:\Users\AB029076\AppData\Local
My Documents :: C:\Users\AB029076\Documents
NetHood :: C:\Users\AB029076\AppData\Roaming\Microsoft\Windows\Network Shortcuts
PrintHood :: C:\Users\AB029076\AppData\Roaming\Microsoft\Windows\Printer Shortcuts
Recent :: C:\Users\AB029076\AppData\Roaming\Microsoft\Windows\Recent
SendTo :: C:\Users\AB029076\AppData\Roaming\Microsoft\Windows\SendTo
Start Menu :: C:\Users\AB029076\AppData\Roaming\Microsoft\Windows\Start Menu
Templates :: C:\Users\AB029076\AppData\Roaming\Microsoft\Windows\Templates
TestLink :: C:\Users\AB029076\AppData\Roaming\Microsoft\Windows\Network Shortcuts
like image 23
abelenky Avatar answered Nov 02 '22 23:11

abelenky