0

A file may be accessed by different paths. For example,

 - subpath/myfile.tex
 - ./subpath/myfile.tex 
 - ../current path/subpath/myfile.tex,
 - /d/.../current path/subpath/myfile.tex

They refer to the same file myfile.tex under current path.

Given these different paths, how can I make a macro to check if they are the the same file?

Example:

\documentclass{article}

\newcommand\ifsamefile[4]{ % if file #1 is in file-list #2(which is a comma list) % then do #3 % else do #4 }

\begin{document}
\ifsamefile{myfile}{./myfile.tex,file2.tex}{true}{false} %should be true
\ifsamefile{myfile}{../current path/myfile.tex,file2.tex}{true}{false} %should be true
\issamefile{subpath/file1.tex}{./subpath/file1.tex,subpath/file2.tex}{true}{false} %should be true
\issamefile{subpath/file1.tex}{/d/mypath/current path/subpath/file1.tex,./subpath/file1.tex}{true}{false} %should be true
\end{document}

lyl
  • 2,727
  • 1
    this is basically a duplicate of your earlier https://tex.stackexchange.com/a/651007/1090 you can get the path the just compare them as token lists – David Carlisle Jul 20 '22 at 07:02
  • 2
    Notice that this is a conceptually difficult thing depending on the operating system and filesystem. In UNIX I can ln filea fileb and then filea and fileb are the same file at all effects. Add in directories symbolically linked and you have a nice mess ;-). – Rmano Jul 20 '22 at 07:08
  • 1
    If I \immediate\openout1 <path1> \immediate\openout2 <path2> for two paths to the same file, my TeX implementation produces a "Runtime error 5" and exits. Perhaps other implementations fail gracefully so that \ifeof2 can be evaluated? That would also catch ln filea fileb cases. – Heiko Theißen Jul 20 '22 at 07:28
  • 1
    Use shell escape and some bash scripting (or Lua scripting). I think TeX doesn't really have a built-in for this. I thought it was about symlinked files, but I see you only consider the same path instead of same file. So yes it's duplicate of your other question. You need an expandable input though. (by the way if you are serious about TeX programming I recommend reading the TeXbook/TeX by topic. Will save you some trouble (and questions) later on.) – user202729 Jul 20 '22 at 08:17

0 Answers0