Code to determine target of remote junction - windows

Windows 7/NTFS and later has both symbolic links and junctions, and they are subtly different. (See this excellent set of posts). For logging (and debugging) purposes, I need to be able to resolve the target of a junction on a remote file server. There have been some posts on this topic, but they apply to resolving a junction as the local machine see it.
On \\FileServer, we have a directory G:\Shared that is shared out as PublicShare, so it apears on the network as \\FileServer\PublicShare. Within that directory are sub-directories G:\Shared\SubDir1 and G:\Shared\SubDir2. Also within that directory is a junction G:\Shared\Junc that points to either G:\Shared\SubDir1 or G:\Shared\SubDir2. (The target can change.) Thus, on \\FileServer, one sees
\\FileServer\PublicShare\SubDir1
\\FileServer\PublicShare\SubDir2
\\FileServer\PublicShare\Junc
On a client machine where \\FileServer\PublicShare is mounted (mapped in Windows lingo) as M:\, one thus sees M:\SubDir1, M:\SubDir2, M:\Junc. If, on that client machine, you open a console (cmd.exe) and do dir M:\, Windows gives a nice listing which shows that M:\Junc is a junction and includes the target, G:\Shared\SubDirX, of the junction.
M:\>dir
Volume in drive M is XXXXXXXXX
Volume Serial Number is XXXX-XXXX
Directory of M:\
09/05/2014 07:30 PM <DIR> .
09/05/2014 07:30 PM <DIR> ..
09/05/2014 01:36 PM <JUNCTION> Junc [G:\Shared\SubDir1]
09/06/2014 12:55 PM <DIR> SubDir1
09/05/2014 05:15 PM <DIR> SubDir2
0 File(s) 0 bytes
3 Dir(s) 1,895,493,492,736 bytes free
M:\>
Thus, the target of such a junction is clearly available to a client. Does anyone know how to obtain this information programatically, e.g. which system api to call??? Thanks.

This does the trick:
fsutil reparsepoint query "M:\Junc"
and it seems very reliable.

If you don't want to parse the output of C:\Windows\System32\fsutil.exe, you could simply use DIR:
for /f "usebackq delims=[] tokens=2" %i in ( `dir M:\^|findstr JUNCTION` ) do set RES=%i
Notes:
M:\: you need to display the subfolders (and junctions) of the parent folder of the JUNCTION (in this case: M:\)
findstr: if you have several junctions and want to resolve a specific one, add a |findstr foldername to isolate the right line,
delims=[]: that will divide the output 09/05/2014 01:36 PM <JUNCTION> Junc [G:\Shared\SubDir1] into 3 tokens:
09/05/2014 01:36 PM <JUNCTION> Junc
G:\Shared\SubDir1
"<empty>"
tokens=2: only the second token ("G:\Shared\SubDir1") is of interest
%i: if you run that command in a .bat script, double the '%': %%i in and res=%%i

Related

cmd command seporator broken in Windows 10

This is unrelated to Java, however the error occurs when I try to compile my code.
The semicolon in the command is causing Windows cmd to fail
Example
c:\>cd c:\windows <- no error
c:\>cd c:\windows; <- the semicolon is not being used a command separator but as part of the path
The system cannot find the path specified.
cmd doesn't use semicolon as a command separator. If you want to have two commands on the same line, you need to use & or | (depending on what you're trying to do).
; is used as an argument separator on Windows, but it's not supported by most applications. It has nothing to do with cmd, and cmd isn't supposed to deal with it (applications parse their command lines, not cmd). It also doesn't apply to cmd intrinsics like cd (cd is not an application).
If you have a directory with a semicolon in the name try wrapping it in "double quotes."
Can't exactly verify your problem, but see below.
Z:\[]>dir
Volume in drive Z has no label.
Volume Serial Number is CAC7-078B
Directory of Z:\
11/27/2019 08:53 <DIR> .
11/27/2019 08:53 <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 871243935744 bytes free
Z:\[]>md zz
Z:\[]>md zz;
A subdirectory or file zz already exists.
Z:\[]>md "zz;"
Z:\[]>dir
Volume in drive Z has no label.
Volume Serial Number is CAC7-078B
Directory of Z:\
11/27/2019 08:54 <DIR> .
11/27/2019 08:54 <DIR> ..
11/27/2019 08:54 <DIR> zz
11/27/2019 08:54 <DIR> zz;
0 File(s) 0 bytes
4 Dir(s) 871243935744 bytes free
Z:\[]>cd zz;
Z:\zz;[]>cd ..\zz
Z:\zz[]>cd "..\zz;"
Z:\zz;[]>
The issue has been resolved with the following
Adding -J-Djdk.lang.Process.allowAmbiguousCommands=true to netbeans_default_options= in etc/netbeans.conf
The output from the compile command has not changed, however it now finds the directories???

What's the difference between Symbolic Link <=> Junction <=> Shortcut?

In Windows using NTFS file system there are 3 different possiblities to create links to folders.
(For files there are 2 possibilities: hard links and shortcuts.)
A shortcut is a .lnk file which has several hundred bytes that contains the link Information.
But what about the other two possibilities: Symbolic Links <=> Junctions?
According to FreeCommander both link "files" need 30 Byte each.
I'm able to create a junction by using
mklink /j LinkFolderToCreate ExistingFolder
To create a symbolic link I use
mklink /d LinkFolderToCreate ExistingFolder
The dir command gives me the following Output:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Temp\LinkTest\Folder>mklink /d SymLink ..\Link
symbolic link created for SymLink <<===>> ..\Link
C:\Temp\LinkTest\Folder>mklink /j Junction ..\Link
Junction created for Junction <<===>> ..\Link
C:\Temp\LinkTest\Folder>dir
Volume in drive C has no label.
Volume Serial Number is 40A4-35D4
Directory of C:\Temp\LinkTest\Folder
30.08.2018 12:09 <DIR> .
30.08.2018 12:09 <DIR> ..
30.08.2018 12:09 <JUNCTION> Junction [C:\Temp\LinkTest\Link]
30.08.2018 12:09 918 Shortcut.lnk
30.08.2018 12:08 <SYMLINKD> SymLink [..\Link]
1 File(s) 918 bytes
4 Dir(s) 63.696.363.520 bytes free
C:\Temp\LinkTest\Folder>
What are the differences between Symbolic Links and Junctions?
Which one would you take in which case?

creating mercurial repo in directory whose name has dollar signs

I am trying to create a mercurial repository in a directory whose name includes dollar signs. This is an equivalent and simplified example of what i get on windows 10 cmd.exe with mercurial 4.1.3 :
C:\test\dir1>hg init
C:\test\dir1>hg status
C:\test\dir1>cd ../dir$$1
C:\test\dir$$1>hg init
C:\test\dir$$1>hg status
abort: repository C:\test\dir$$1 not found!
so i hope this is clear, the only difference seems to be the dollar signs in the second directory name. thanks in advance!
Mercurial seems to treat dollar signs as an environment variable escape:
C:\test>set X=abc
C:\test>echo $X # Not the shell expanding it, that would be %X%.
$X
C:\test>hg init $X
C:\test>dir
Volume in drive C has no label.
Volume Serial Number is CE8B-D448
Directory of C:\test
11/10/2017 09:27 PM <DIR> .
11/10/2017 09:27 PM <DIR> ..
11/10/2017 09:27 PM <DIR> abc
0 File(s) 0 bytes
3 Dir(s) 53,899,231,232 bytes free
Mercurial has expanded $X as an environment variable. Also:
C:\test>hg init dir$$x
C:\test>dir
Volume in drive C has no label.
Volume Serial Number is CE8B-D448
Directory of C:\test
11/10/2017 09:30 PM <DIR> .
11/10/2017 09:30 PM <DIR> ..
11/10/2017 09:30 PM <DIR> dir$x
0 File(s) 0 bytes
3 Dir(s) 53,899,091,968 bytes free
Two dollar signs insert one dollar sign. When you are in a directory named dir$$x, Mercurial is using dir$x for the name. I found a workaround with hg -R. status, but better to avoid dollar signs.
this has been entered as a Mercurial bug : https://bz.mercurial-scm.org/show_bug.cgi?id=5739

copying files from windows cmd.exe [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I am trying to make a backup of the files in a corrupted MySQL database before doing a clean install from a different backup file. I am on a windows 7 machine. Someone suggested the following, but the mv command does not work on windows:
mv /var/lib/mysql /var/lib/mysql.old
So instead, I am trying to use robocopy, but I am getting a range of different error messages with each attempt. Here is a screen shot of some of the error messages for my various attempts:
Here is a link to the complete suggested code in a related posting, if it helps anyone suggest the correct syntax: Click Here
I tried xcopy but am getting the following error result:
Then I tried move but got the following results, despite trying a few different ways of doing the slashes:
I added a C:\ProgramData\MySQL\bkup folder to my hard drive and tried the following:
why don't use this syntax ? :
xcopy /s SOURCEDIR DESTDIR
eg: xcopy /s C:\Users\Iman\Desktop\k C:\Users\Iman\Desktop\kk
Here's a possible approach, since the paths seem to be crossing you up:
MD C:\BackupDir
Subst G: c:\BackupDir
Cd C:\MypathtoMysql
G: (Switch to G drive)
Xcopy C:*.* /s /e
That way you're in the directory you want to copy so no need to specify it, and you're copying to a local location rather than a path location.
So, I've done this to prove what I tried to convey in the comments:
Created a directory called C:\ProgramData\directory 1\sub-directory a, and two files in it:
C:\>dir "C:\ProgramData\directory 1\sub-directory a"
Volume in drive C has no label.
Volume Serial Number is 303C-DB3D
Directory of C:\ProgramData\directory 1\sub-directory a
24/05/2014 22:35 <DIR> .
24/05/2014 22:35 <DIR> ..
24/05/2014 22:35 13 file 1
24/05/2014 22:35 13 file 2
2 File(s) 26 bytes
2 Dir(s) 1,076,169,957,376 bytes free
Created a directory C:\ProgramData\directory 2:
C:\>dir "C:\ProgramData\directory 2"
Volume in drive C has no label.
Volume Serial Number is 303C-DB3D
Directory of C:\ProgramData\directory 2
24/05/2014 22:35 <DIR> .
24/05/2014 22:35 <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 1,076,169,957,376 bytes free
Used move to move sub-directory a to C:\ProgramData\directory 2 like so:
C:\>move "C:\ProgramData\directory 1\sub-directory a" "C:\ProgramData\directory 2\sub-directory a"
1 dir(s) moved.
Confirmed that the directory and its files are where they should:
C:\>dir "C:\ProgramData\directory 2\sub-directory a"
Volume in drive C has no label.
Volume Serial Number is 303C-DB3D
Directory of C:\ProgramData\directory 2\sub-directory a
24/05/2014 22:35 <DIR> .
24/05/2014 22:35 <DIR> ..
24/05/2014 22:35 13 file 1
24/05/2014 22:35 13 file 2
2 File(s) 26 bytes
2 Dir(s) 1,076,169,957,376 bytes free
In conclusion:
move can be said to be a renaming operation: The first argument, the source, is the path of the file right now, the second argument, the destination, is what you want it to be.
The parent directory of the destination should exist. E.g. if I move to C:\ProgramData\directory 2\sub-directory a there should already be a directory C:\ProgramData\directory 2.
Arguments that contain a space need to be quoted, like the directory arguments to move. Each argument should be quoted on its own.
Use relative paths when you're sure about what they should be, otherwise use absolute paths.
Is there a reason why you want this done via command line? Why not use a file-diff tool such as Beyond Compare? This tool gives you a visual overview of file differences, but can be used to copy files to another folder for backup as well. Full binary comparison of files in different folders is possible too. See a screenshot. There is a command line version included.
Perhaps this topic from SuperUser can help: https://superuser.com/questions/748069/how-do-i-compare-two-folders-recursively-and-generate-a-list-of-files-and-folder - look down the list of answers and there will be an entry on RoboCopy and its use for a somewhat similar purpose.

Why does the cmd.exe shell on Windows fail with paths using a forward-slash ('/'') path separator?

Just when I'd thought I'd seen it all with Windows path issues, I've now encountered a case that only fails when '/' (forward-slash) is used as the path separator is used:
C:\temp\tcbugs>mkdir "dir1 with spaces"
C:\temp\tcbugs>echo hi > "dir1 with spaces"\foo.txt
C:\temp\tcbugs>type "dir1 with spaces\foo.txt"
hi
C:\temp\tcbugs>type "dir1 with spaces/foo.txt"
The system cannot find the file specified.
What is particularly interesting about this is that it appears to be specific to the cmd.exe shell and doesn't occur in PowerShell (nor presumably in the win32 API):
PS C:\temp\tcbugs> type 'dir1 with spaces/foo.txt'
hi
Another point of interest is that changing directories with 'cd' and using '/' used as a path separator with cmd.exe does work:
C:\temp\tcbugs>mkdir dir2_no_spaces
C:\temp\tcbugs>cd ./dir2_no_spaces
C:\temp\tcbugs\dir2_no_spaces>cd ..
Yet, I can't find any reference to this particular problem anywhere online nor in MSDN's commonly cited documentation:
Naming Files, Paths, Namespaces
Which leads me to ask: why does this happen, and is there a definitive source which documents this quirk?
UPDATE:
dbenham points out that the problem is present regardless of whether spaces are in a directory name, so removed reference to it in the title and question body. Also added an example of 'cd ./' that works, while other commands don't.
Edited to remove opinion
Whether or not Windows CMD.EXE is supposed to support forward slashes in paths, the fact is sometimes it works, sometimes it doesn't, and sometimes it appears to work but gives the wrong result - AKA a bug.
It's time for some experiments :-)
All tests were run on Vista
C:\>md "c:/temp/"
C:\>REM The forward slash works with MD!
C:\>echo hello world 1>>"c:/temp/test.txt"
C:\>REM Redirection works with forward slashes!
C:\>type "c:\temp\test.txt"
hello world
C:\>REM Of course TYPE works with back slashes
C:\>type "c:/temp/test.txt"
The system cannot find the file specified.
C:\>REM But forward slash version fails
C:\>type "c:/temp\test.txt"
hello world
C:\>REM But TYPE works with forward slash as long as last slash is back slash
C:\>dir "c:/temp/test.txt"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp
File Not Found
C:\>REM Note how DIR lists the directory with a \, yet fails to find any files
C:\>dir "c:/temp/*"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp
File Not Found
C:\>REM DIR Still fails with forward slashes
C:\>dir "c:/temp/"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp
05/09/2012 09:58 PM <DIR> .
05/09/2012 09:58 PM <DIR> ..
05/09/2012 09:58 PM 13 test.txt
1 File(s) 13 bytes
2 Dir(s) 337,001,615,360 bytes free
C:\>REM But forward slash works if no file is specified!
C:\>dir "c:/temp\test.txt"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp
05/09/2012 09:58 PM 13 test.txt
1 File(s) 13 bytes
0 Dir(s) 337,001,615,360 bytes free
C:\>REM And DIR works with forward slash as long as last slash is back slash
C:\>REM Now add another folder to the path hierarchy
C:\>md "c:/temp/temp/"
C:\>REM Still can create folder using forward slashes
C:\>copy "c:/temp/test.txt" "c:/temp/temp/"
The system cannot find the file specified.
0 file(s) copied.
C:\>REM Failed to copy with forward slashes
C:\>copy "c:/temp\test.txt" "c:/temp/temp/"
1 file(s) copied.
C:\>REM But forward slash works if last slash before file name is back slash
C:\>REM Rerun some past tests
C:\>type "c:/temp/test.txt"
The system cannot find the file specified.
C:\>REM Good - it still fails
C:\>dir "c:/temp/test.txt"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp
05/09/2012 09:58 PM 13 test.txt
1 File(s) 13 bytes
0 Dir(s) 337,001,615,360 bytes free
C:\>REM What is going on?! :( Why did that seem to work now?
C:\>REM More on that later.
C:\>REM Now test the new folder
C:\>type "c:/temp/temp/test.txt"
The system cannot find the file specified.
C:\>REM Forward slashes still fail with TYPE
C:\>type "c:/temp/temp\test.txt"
hello world
C:\>REM But forward slash still works as long as last slash is back slash
C:\>dir "c:/temp/temp/*"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp\temp
File Not Found
C:\>REM Again, forward slashes fail, but directory path is listed properly
C:\>dir "c:/temp/temp/"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp\temp
05/09/2012 09:58 PM <DIR> .
05/09/2012 09:58 PM <DIR> ..
05/09/2012 09:58 PM 13 test.txt
1 File(s) 13 bytes
2 Dir(s) 337,001,615,360 bytes free
C:\>REM And again it works if no file is specified
C:\>dir "c:/temp/temp\test.txt"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp\temp
05/09/2012 09:58 PM 13 test.txt
1 File(s) 13 bytes
0 Dir(s) 337,001,615,360 bytes free
C:\>REM Again forward slashes work as long as last slash is back slash
Here is a case that clearly demonstrates a bug.
c:\>dir /s /a-d temp
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp
05/10/2012 08:01 AM 13 test.txt
1 File(s) 13 bytes
Directory of c:\temp\temp
05/10/2012 07:57 AM 10 test.txt
1 File(s) 10 bytes
Total Files Listed:
2 File(s) 23 bytes
0 Dir(s) 337,325,191,168 bytes free
c:\>REM Note the different file sizes found in each directory
c:\>dir "c:/temp/test.txt"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp
05/10/2012 07:57 AM 10 test.txt
1 File(s) 10 bytes
0 Dir(s) 337,325,191,168 bytes free
c:\>REM It is listing the wrong file!
One can debate whether Windows CMD is "supposed" to support forward slashes. But that last result is a bug! Even if there is operator error in using a forward slash, Windows should not give that result.
Wie had some strange behaviour with slash to and we traced it down to the fact, that a path with a leading slash isn't seen as an absolute path, so
C:\>cd /temp
C:\temp>rem works we are in the root directory
C:\temp>cd /temp
Das System kann den angegebenen Pfad nicht finden.
C:\temp>rem does't work but
C:\temp>cd \temp
C:\temp>rem \ indicates absolute path
C:\temp>cd ..
C:\>cd /temp
C:\temp> cd /ca
C:\temp\CA>rem qed
Perhaps it explains also the bug stated above - im not clear, at which directory the commands are executed.
I am not sure why the '/' is working in PS. Back to the history, DOS was based on UNIX and it is a small set of UNIX. In UNIX the path separator is '/', while in DOS it is '\'. I worked on some Windows and DOS apps before. In order to convert some UNIX pattern like commands or path and make sure they are valid DOS command or path, I wrote a small converter to transform '/' to '\' like this:
string fileNameFromWeb;
...
string windowsFile = fileNameFromWeb.repleace("/", #"\");
You may add this feature to tolerant '/' in your app in case of accessing to files in Windows. I guess PS may have this type converter to allow command or path using '/' or '\', or Windows will take '/' in file name.

Resources