Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @echo off
- setlocal enabledelayedexpansion
- rem MoveToTree.bat v1.0.1.
- rem
- rem Move files in current directory into directory tree according to "tree.txt" file which was made using "tree /a /f" command.
- rem
- rem Context: https://old.reddit.com/r/CodingHelp/comments/16h94fs/how_to_sort_all_files_in_accordance_with_treetxt/
- rem
- rem Usage:
- rem
- rem For testing purpose. Simulate moving files to tree. Does not actually move the files, but it does check for files presence.
- rem MoveToTree.bat
- rem
- rem Actually move files into directory tree.
- rem MoveToTree.bat doit
- rem
- rem Files which need to be moved into tree along with the "tree.txt" file, must be in the current directory.
- rem Script will create all tree subdirectories as needed.
- rem Script will terminate if "tree.txt" file format is invalid or is not in English language.
- rem Script will terminate if any line in "tree.txt" is unrecognized.
- rem Script will terminate if it can not create needed subdirectory or can not move needed file.
- rem Script will report any missing file, but will not terminate the script if a file is missing.
- if "%~1" == "doit" (
- set doit=1
- ) else set doit=0
- if not exist tree.txt (
- echo "tree.txt" file is not found.
- goto :eof
- )
- set dirLevels=-1
- set ln=1
- for /f "delims=" %%A in (tree.txt) do (
- set "l=%%A"
- if !ln! == 1 (
- if "!l:~0,19!" neq "Folder PATH listing" (
- echo Invalid tree file.
- goto :eof
- )
- ) else if !ln! gtr 3 (
- if "!l:~-1!" neq "|" (
- call :processLine
- if errorlevel 1 goto :eof
- )
- )
- set /a ln+=1
- )
- goto :eof
- :processLine
- set type=file
- set lv=0
- :chklvl
- set /a i=lv*4
- set "c=!l:~%i%,1!"
- if "%c%" == " " (
- set /a lv+=1
- goto chklvl
- )
- if "%c%" == "|" (
- set type=file
- set /a lv+=1
- goto :chklvl
- ) else if "%c%" == "\" (
- set type=dir
- set /a lv+=1
- goto :chklvl
- ) else if "%c%" == "+" (
- set type=dir
- set /a lv+=1
- goto :chklvl
- ) else if "%type%" == "" (
- echo ERROR: Unrecognized line at line %ln%.
- exit /b 1
- )
- set "name=!l:~%i%!"
- if "%name%" neq "" (
- set /a lv-=1
- ) else (
- set type=ignore
- goto :eof
- )
- if "%type%" == "dir" (
- set dirLevels=%lv%
- set "dir%lv%=%name%"
- )
- set dir=
- for /l %%B in (0,1,%dirLevels%) do set "dir=!dir!!dir%%B!\"
- if "%type%" == "file" goto file
- :dir
- echo "%dir%"
- if %doit% == 1 (
- if not exist "%dir%" (
- md "%dir%"
- if errorlevel 1 exit /b 1
- )
- )
- goto :eof
- :file
- echo "%dir%%name%"
- if exist "%name%" (
- if %doit% == 1 (
- >nul move "%name%" "%dir%"
- if errorlevel 1 exit /b 1
- )
- ) else echo ERROR: File "%name%" is not found.
- goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement