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.txtD:\vehicles\345678\key\that.txt
symbolic linked
e:\new\redcar1\e:\new\greencar2\e:\new\keys\this.txte:\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.
This may help to get symlinks to the files
this.txtandthat.txt. You have akeysfolder and akeyfolder, yet your code only searches the directory of one of them. So nowdirwill search both directories.I also adjusted quoting and use
~in theforvariables 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^>nulin theforcommands is to redirect stderr stream tonul, so output of no files found bydirwill not be output.usebackqinforoptions changes the rules for the command. Double quotes withoutusebackq, interprets a double quotes as a string, not a path. Withusebackq, double quotes can be used on a path.%~dp0is a path that may have spaces so double quoting seems a good idea to add. Viewfor /?for more help.