windows - Batch - Moving file up a level if it's the only file in the directory -
i have several hundred folders. of folders have 1 file in them. if folder contains 1 file, i'd move file directory, it's parent file, , delete empty folder. hope i'm making question clear. i'm assuming best way this, batch file? i'm on windows.
- root directory - folder 1 - file 1* - folder 2 - file 1 - file 2 - folder 3 - file 1* - folder 4 - file 1 - file 2 - file 3
i'd files asterisks moved root directory.
this should out. few notes it's doing.
- the first loop walks directory tree current directory
- the second loop counts files (no directories), if there 1 file moved parent directory
- the 3rd loop counts both files , subdirectories, , tries removing if empty
you should find running multiple times move single file multiple levels of directory tree - if it's file @ each level.
@echo off setlocal enabledelayedexpansion /r /d %%d in (*) ( /f %%f in ('dir /b /a-d "%%d\*" 2^>nul ^| find /c /v ""') set filecount=%%f if !filecount! equ 1 move "%%d\*" "%%~dpd" /f %%f in ('dir /b "%%d\*" 2^>nul ^| find /c /v ""') set filecount=%%f if !filecount! equ 0 rd "%%d" )
Comments
Post a Comment