Batch Script to Move Folders and Create Hidden Symlinks
For my first post on my new blog, I decided to start simple and showcase some quick code I wrote to enhance my organization techniques. This batch file reorganizes a user's Documents folder by removing clutter from software-created folders. It does not interrupt the software's use of these folders because it simply moves the software's folder into a designated Software folder, creates a symbolic link in its original place within the Documents folder, and hides it—leaving only the folders Academics, Personal, Professional, and Software (my personal sorting method).
1@echo off
2setlocal enabledelayedexpansion
3
4REM Set the Documents directory path
5set "documentsDir=%USERPROFILE%\Documents"
6
7REM Set the Software directory path
8set "softwareDir=%USERPROFILE%\Documents\Software"
9
10REM Change to the Documents directory
11cd /d "%documentsDir%"
12
13REM Loop through the folders in the Documents directory
14for /d %%f in (*) do (
15 REM Get the folder name
16 set "folderName=%%~nxf"
17
18 REM Skip "Academics", "Personal", "Professional", and "Software"
19 if /i "!folderName!" neq "Academics" (
20 if /i "!folderName!" neq "Personal" (
21 if /i "!folderName!" neq "Professional" (
22 if /i "!folderName!" neq "Software" (
23
24 REM Move the folder to the Software directory
25 move "!documentsDir!\!folderName!" "!softwareDir!\" >nul 2>&1
26
27 REM Check if the move operation was successful
28 if errorlevel 1 (
29 echo Failed to move folder !folderName!.
30 exit /b 1
31 )
32
33 REM Create the symbolic link in the Documents directory pointing to the folder in Software
34 mklink /d "!documentsDir!\!folderName!" "!softwareDir!\!folderName!" >nul 2>&1
35
36 REM Check if mklink was successful
37 if errorlevel 1 (
38 echo Failed to create symbolic link for !folderName!.
39 exit /b 1
40 )
41
42 REM Hide the symbolic link
43 attrib +h "!documentsDir!\!folderName!" /l >nul 2>&1
44
45 REM Check if attrib was successful
46 if errorlevel 1 (
47 echo Failed to hide the symbolic link for !folderName!.
48 exit /b 1
49 )
50
51 )
52 )
53 )
54 )
55)
56
57echo Symbolic links created, folders moved, and links hidden successfully.
58pause
Some future improvements might include the automatic creation of the Academics, Personal, etc., folders if they are not already in the Documents folder. Another feature could allow a user to select which folders they do not want to move to Software, in case they use a different organization structure.