Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Native alternative for readlink on Windows

Windows native alternative for ln -s is mklink.

Is there any native alternative for readlink? Or how to natively identify if the file is symlink?

like image 507
Honza Štefl Avatar asked Nov 08 '12 21:11

Honza Štefl


1 Answers

No need to redirect the stdout to a temp.txt file. You can simply do this in one line:

for /f "tokens=6" %i in ('dir mysymlink ^| FIND "<SYMLINK>"') do @echo %i

Also note that the above method by Lars only applies to "file" symlinks. That method has to be adjusted for directory symlinks since passing a directory to the "dir" command will list out that directories actual contents (which won't have itself in there)

So you would use:

for /f "tokens=6" %i in ('dir mysymlink* ^| FIND "<SYMLINKD>"') do @echo %i

Note the inclusion of the * in the dir call and the addition of the "D" in the FIND

The asterisk will prevent dir from listing the contents of that directory.

To further simplify the process to work for either file or directory symlinks, you would simply always include the asterisk and for the FIND argument you would change it to a partial match so that it matches with or without "D" on the end

for /f "tokens=6" %i in ('dir mysymlink* ^| FIND "<SYMLINK"') do @echo %i

And finally, since that method actually returns the file inside the brackets [].. you would be better off delimiting those, leaving you with the final method:

for /f "tokens=2 delims=[]" %i in ('dir mysymlink* ^| FIND "<SYMLINK"') do @echo %i
like image 80
Dss Avatar answered Sep 27 '22 18:09

Dss