-1

I create a lot of hardlinks every week. When time comes to clean them, I find myself using the "DeleteAllHardlinks.bat" for ln (https://schinagl.priv.at/nt/ln/ln.html) but I have to drag and drop everyfile one after the other.

I would love to find a way to just select 100 files and drop them on the .bat, wait a while and find all those files and hardlinks deleted for good. Is there anyway to change the .bat file to allow this? (or maybe any other different method to acomplish the same?)

@echo off

REM
REM Check for commandline args
REM
if "[%~1]" == "[]" goto error

set LN=ln.exe

REM
REM List hardlink sibblings and delete all siblings
REM
for /f "delims=" %%a in ('@%LN% --list "%~1"') do (
  del /f "%%a"
)

goto ausmausraus

:error
echo DeleteAllHardlinks: Argument is missing. Usage DeleteAllHardlinks ^<filename^>
echo e.g. DeleteAllHardlinks c:\data\myfile.txt

:ausmausraus
echo on

Thanks in advance!

keiju
  • 1
  • 1
  • @keiju `ln.exe` is not a standard [Windows command](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands) output on running `help` in a command prompt window or the referenced Microsoft documentation page. It is also not listed on [SS64.com - A-Z index of Windows CMD commands](https://ss64.com/nt/) which has the best documentations for all Windows commands including [mklink](https://ss64.com/nt/mklink.html) to create a directory symbolic link, a hard link or a directory junction. – Mofi Dec 09 '20 at 12:02
  • @keiju `dir /AL` can be used to get listed all directory symbolic links and directory junctions in a directory as described by `dir /?`. However, the batch file could be very easily modified to support not just first argument, but all file name argument strings passed to the batch file by using one more `for` loop and `%*` as explained by `call /?`, i.e. `for %%I in (%*) do for /F "delims=" %%J in ('ln.exe --list "%%~I" 2^>nul') do del /F "%%~J"` as replacement for the existing `for` loop. But the app starting the bat has to pass each file name enclosed in double quotes to work properly. – Mofi Dec 09 '20 at 12:10

1 Answers1

0

Big thanks to Mofi!

The batch file could be very easily modified to support not just first argument, but all file name argument strings passed to the batch file by using one more for loop and %* as explained by call /?, i.e. use as replacement for the existing for loop:

for %%I in (%*) do for /F "delims=" %%J in ('ln.exe --list "%%~I" 2^>nul') do del /F "%%~J"

But the application starting the batch file has to pass each file name enclosed in double quotes to work properly.

Just using the for as offered in the comment solved the issue perfectly.

Mofi
  • 46,139
  • 17
  • 80
  • 143
keiju
  • 1
  • 1