readlink emulation: first approach

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
siberia-man
Posts: 208
Joined: 26 Dec 2013 09:28
Contact:

readlink emulation: first approach

#1 Post by siberia-man » 07 Mar 2016 03:59

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

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: readlink emulation: first approach

#2 Post by sambul35 » 07 Mar 2016 07:09

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.

siberia-man
Posts: 208
Joined: 26 Dec 2013 09:28
Contact:

Re: readlink emulation: first approach

#3 Post by siberia-man » 08 Mar 2016 03:35

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.

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: readlink emulation: first approach

#4 Post by jeb » 08 Mar 2016 11:28

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?

siberia-man
Posts: 208
Joined: 26 Dec 2013 09:28
Contact:

Re: readlink emulation: first approach

#5 Post by siberia-man » 08 Mar 2016 23:31

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.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: readlink emulation: first approach

#6 Post by Compo » 09 Mar 2016 04:15

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)

b0gus
Posts: 7
Joined: 02 Mar 2016 12:58

Re: readlink emulation: first approach

#7 Post by b0gus » 09 Mar 2016 06:36

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>
Last edited by b0gus on 09 Mar 2016 09:31, edited 1 time in total.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: readlink emulation: first approach

#8 Post by Compo » 09 Mar 2016 07:41

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!

Post Reply