Page 1 of 1

readlink emulation: first approach

Posted: 07 Mar 2016 03:59
by siberia-man
Unix has the command readlink for printing values of symbolic links. Windows doesn't have.

This post is the first attempt to close this lack.

Code: Select all

@echo off

for /f "tokens=*" %%f in ( '
   dir /b /al "%~1*"
' ) do for /f "tokens=1,2,3 delims=[]" %%a in ( '
   dir /al "%~dp1%%f*"  ^| findstr "<SYMLINK> <SYMLINKD> <JUNCTION>"
' ) do (
   echo:%%~b
)


Example:
C:\>dir /b /al
Documents and Settings

C:\>readlink
C:\Users


Known issues:
1. incorrect execution if names contain symbols "[" and/or "]"
2. could duplicate items in the output
3. false shots in the case of looking for existing symlinks in nested directories and ran in the exact form of readlink c:

This post was inspired by this thread: http://stackoverflow.com/questions/1329 ... s/13358046
The same discussion was opened on Russian forum: http://forum.script-coding.com/viewtopic.php?id=11385

Re: readlink emulation: first approach

Posted: 07 Mar 2016 07:09
by sambul35
Could you give some idea, in what tasks printing values of symbolic links may be required. It may help other batchers to use your code for accomplishing their tasks.

Re: readlink emulation: first approach

Posted: 08 Mar 2016 03:35
by siberia-man
sambul35, good point.

Of course, symbolic links are needed mostly for compatibility purposes. For example, if some application has to do with the specific directory structure and we need to keep it working in the new environment. I guess "C:\Documents and Settings" pointing to "C:\Users" is that case. And of course, in the most of cases we don't need to worry about the target file/directory. Knowledge of the target of symbolic link could be important in the case when we need to include or exclude those files/directories. So, for example, backing up some source to another destination we'd like to exclude any symlinks or exclude only those symlinks that point to the directory structure within the source.

I have just described my case. I am using nnBackup tool for backing up some vital working environment. This tool completely satisfies my requirements besides of impossibility to handle symlinks. Looking on xcopy/robocopy we can see that both of them are not able to cover expected functionality.

Re: readlink emulation: first approach

Posted: 08 Mar 2016 11:28
by jeb
Good idea, but ...

it's depends on the local language, on my (german) system I got
14.07.2009 06:08 <VERBINDUNG> Documents and Settings [C:\Users]


I don't know what texts are also possible instead of "VERBINDUNG".

Why do you check against "<SYMLINK> <SYMLINKD> <JUNCTION>" at all?

Re: readlink emulation: first approach

Posted: 08 Mar 2016 23:31
by siberia-man
jeb, this is bad news. They seem to try localize everything possible. VERBINDUNG is the German translation for JUNCTION. I am using non-localized version of Windows where all symbolic links are marked as SYMLINK, SYMLINKD and JUNCTION. I need to check how these markers are presented in Russian. Most probably it would be enough to filter with the pattern "<.*>" or something else.

Re: readlink emulation: first approach

Posted: 09 Mar 2016 04:15
by Compo
jeb wrote:Why do you check against "<SYMLINK> <SYMLINKD> <JUNCTION>" at all?

@siberia-man, I think the point jeb is making is that the /AL switch only outputs 'symlinks' so why are you performing the check for them?

If you were just trying to isolate the pertinent lines, i.e. remove the information at the head and tail, there appears to be a way of doing that because all of those lines have a commonality.

e.g. (command run from console at %userprofile%)

Code: Select all

for /f "delims= eol= " %a in ('dir/al') do @(for /f "tokens=3*" %b in ("%a") do @echo=%c)

Re: readlink emulation: first approach

Posted: 09 Mar 2016 06:36
by b0gus
siberia-man wrote:jeb, this is bad news.

may be - no?
to jeb, try this:

Code: Select all

>nul chcp 437
dir /al
>nul chcp <your codepage>

Re: readlink emulation: first approach

Posted: 09 Mar 2016 07:41
by Compo
as a follow up to my last post, you could maybe use something similar to this to isolate and output them:

Get-SLinx.cmd (requires input parameter)

Code: Select all

@echo off
setlocal enabledelayedexpansion
set _=0
for /f "delims= eol= " %%a in ('dir/al "%~1"') do (
   for /f "tokens=3*" %%b in ("%%a") do (set/a _+=1
      set _f=%%c
      set _f!_!=!_f:~,-1!))
set _=0
for /f "delims=" %%a in ('dir/b/al "%~1"') do (set/a _+=1
   set _b!_!=%%~nxa [)
for /l %%a in (1,1,%_%) do call :sub "%%_f%%a%%" "%%_b%%a%%"
timeout -1
exit/b
:sub
set _f=%~1
set _b=%~2
echo=%_b:~,-2% =^> !_f:*%_b%=!
You could even change both commands to 'dir/s/al' and 'dir/b/s/al' and use %systemdrive% as the input parameter to list all on your drive!