Advertisement
jcunews

MoveToTree.bat

Sep 14th, 2023 (edited)
1,357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 2.61 KB | None | 0 0
  1. @echo off
  2. setlocal enabledelayedexpansion
  3.  
  4. rem MoveToTree.bat v1.0.1.
  5. rem
  6. rem Move files in current directory into directory tree according to "tree.txt" file which was made using "tree /a /f" command.
  7. rem
  8. rem Context: https://old.reddit.com/r/CodingHelp/comments/16h94fs/how_to_sort_all_files_in_accordance_with_treetxt/
  9. rem
  10. rem Usage:
  11. rem
  12. rem   For testing purpose. Simulate moving files to tree. Does not actually move the files, but it does check for files presence.
  13. rem     MoveToTree.bat
  14. rem
  15. rem   Actually move files into directory tree.
  16. rem     MoveToTree.bat doit
  17. rem
  18. rem Files which need to be moved into tree along with the "tree.txt" file, must be in the current directory.
  19. rem Script will create all tree subdirectories as needed.
  20. rem Script will terminate if "tree.txt" file format is invalid or is not in English language.
  21. rem Script will terminate if any line in "tree.txt" is unrecognized.
  22. rem Script will terminate if it can not create needed subdirectory or can not move needed file.
  23. rem Script will report any missing file, but will not terminate the script if a file is missing.
  24.  
  25. if "%~1" == "doit" (
  26.   set doit=1
  27. ) else set doit=0
  28.  
  29. if not exist tree.txt (
  30.   echo "tree.txt" file is not found.
  31.   goto :eof
  32. )
  33.  
  34. set dirLevels=-1
  35. set ln=1
  36. for /f "delims=" %%A in (tree.txt) do (
  37.   set "l=%%A"
  38.   if !ln! == 1 (
  39.     if "!l:~0,19!" neq "Folder PATH listing" (
  40.       echo Invalid tree file.
  41.       goto :eof
  42.     )
  43.   ) else if !ln! gtr 3 (
  44.     if "!l:~-1!" neq "|" (
  45.       call :processLine
  46.       if errorlevel 1 goto :eof
  47.     )
  48.   )
  49.   set /a ln+=1
  50. )
  51. goto :eof
  52.  
  53. :processLine
  54. set type=file
  55. set lv=0
  56. :chklvl
  57. set /a i=lv*4
  58. set "c=!l:~%i%,1!"
  59. if "%c%" == " " (
  60.   set /a lv+=1
  61.   goto chklvl
  62. )
  63. if "%c%" == "|" (
  64.   set type=file
  65.   set /a lv+=1
  66.   goto :chklvl
  67. ) else if "%c%" == "\" (
  68.   set type=dir
  69.   set /a lv+=1
  70.   goto :chklvl
  71. ) else if "%c%" == "+" (
  72.   set type=dir
  73.   set /a lv+=1
  74.   goto :chklvl
  75. ) else if "%type%" == "" (
  76.   echo ERROR: Unrecognized line at line %ln%.
  77.   exit /b 1
  78. )
  79. set "name=!l:~%i%!"
  80. if "%name%" neq "" (
  81.   set /a lv-=1
  82. ) else (
  83.   set type=ignore
  84.   goto :eof
  85. )
  86. if "%type%" == "dir" (
  87.   set dirLevels=%lv%
  88.   set "dir%lv%=%name%"
  89. )
  90. set dir=
  91. for /l %%B in (0,1,%dirLevels%) do set "dir=!dir!!dir%%B!\"
  92. if "%type%" == "file" goto file
  93.  
  94. :dir
  95. echo "%dir%"
  96. if %doit% == 1 (
  97.   if not exist "%dir%" (
  98.     md "%dir%"
  99.     if errorlevel 1 exit /b 1
  100.   )
  101. )
  102. goto :eof
  103.  
  104. :file
  105. echo "%dir%%name%"
  106. if exist "%name%" (
  107.   if %doit% == 1 (
  108.     >nul move "%name%" "%dir%"
  109.     if errorlevel 1 exit /b 1
  110.   )
  111. ) else echo ERROR: File "%name%" is not found.
  112. goto :eof
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement