0

I have been trying to make symbolic links from multiple sources.

I have a reg.txt file in the new directory as well as the sort.bat.

the reg.txt :-

redcar1=123456
greencar2=345678

In directory called vehicles with sub directories of 'reg_numbers' ie:- 123456, within that directory I have a sub directory called keys, or key, with a *.txt file.

I want to symbolic link all the 'reg_numbers' (parsing the reg.txt - changing the folder name in the process) directories into the new directory the sort.bat file is in, and all the keys or Key directory files (*.txt), into a common keys directory in the new folder

so say:-

  • D:\vehicles\123456\keys\this.txt
  • D:\vehicles\345678\key\that.txt

symbolic linked

  • e:\new\redcar1\
  • e:\new\greencar2\
  • e:\new\keys\this.txt
  • e:\new\keys\that.txt

sort.bat is in the new folder.

@echo off
setlocal EnableDelayedExpansion

set NEWPATH=%~dp0
set OLDPATH=d:\vehicles

for /f %%d in ('dir /b %NEWPATH%\keys') do (
    if not "%%d"=="not.txt" del /Q "%NEWPATH%Keys\%%d" ::clean out files
)
for /f "tokens=1,2 delims=="  %%a in (reg.txt) do (
    rmdir /Q "%NEWPATH%%%a" ::clean out dir
    mklink /D %NEWPATH%"%%a" %OLDPATH%\"%%b"
    for /f %%c in ('dir /b %OLDPATH%\%%b\keys' ) do (

    mklink %NEWPATH%Keys\"%%c" %OLDPATH%\"%%b"\"%%c" 
    )
)

It's probably a mess but it works for nearly everything but I cannot get the that.txt from key folder.

michael_heath
  • 5,262
  • 2
  • 12
  • 22
marc
  • 1
  • 1
    Suggest you name *sort.bat* to something else as it has the same name as *sort.exe* if you use the command `sort`. Perhaps a name that is more unique. – michael_heath Mar 29 '20 at 08:41

1 Answers1

0
@echo off
setlocal EnableDelayedExpansion

set "NEWPATH=%~dp0"
set "OLDPATH=D:\vehicles"

for /f %%A in ('2^>nul dir /b /a-d "%NEWPATH%\keys"') do (
    if not "%%~A" == "not.txt" del /Q "%NEWPATH%\Keys\%%~A"
)

for /f "usebackq tokens=1,2 delims==" %%A in ("%NEWPATH%\reg.txt") do (
    rmdir /Q "%NEWPATH%\%%~A"
    mklink /D "%NEWPATH%\%%~A" "%OLDPATH%\%%~B"

    for /f %%C in ('2^>nul dir /b "%OLDPATH%\%%~B\keys" "%OLDPATH%\%%~B\key"' ) do (
        mklink "%NEWPATH%\Keys\%%~C" "%OLDPATH%\%%~B\%%~C"
    )
)

This may help to get symlinks to the files this.txt and that.txt. You have a keys folder and a key folder, yet your code only searches the directory of one of them. So now dir will search both directories.

I also adjusted quoting and use ~ in the for variables to remove possible quotes. Backslashes added as path separators where needed.

The double quotes for the set commands is to ensure that the value does not include any trailing space that is invisible to see.

2^>nul in the for commands is to redirect stderr stream to nul, so output of no files found by dir will not be output.

usebackq in for options changes the rules for the command. Double quotes without usebackq, interprets a double quotes as a string, not a path. With usebackq, double quotes can be used on a path. %~dp0 is a path that may have spaces so double quoting seems a good idea to add. View for /? for more help.

michael_heath
  • 5,262
  • 2
  • 12
  • 22
  • Thanks for the help, and it works. As a beginner, i will research why you quoted the ```set ""``` and added ```'2^>nul``` and also ```usebackq```. i new i had missed the ```key``` folder, but could not work out how to add it. – marc Mar 29 '20 at 12:02