简体   繁体   中英

Batch file to move bottom folder into specified location

I have a directory structure which contains files organized into folders by an associated ID

..\abc\STORAGE\123
..\abc\STORAGE\234
..\xyz\STORAGE\345
..\xyz\STORAGE\456

I want all of the bottom subfolders, and all their contents, to be copied into a new directory such that afterwards I will have:

..\SomeNewDir\123
..\SomeNewDir\234
..\SomeNewDir\345
..\SomeNewDir\456

How should I accomplish this, using a batch script?

Test this: launch it in the folder that holds the abc and xyz folders.

@echo off
for /d %%a in (*) do xcopy "%%a\storage\*.*" "c:\somenewdir\" /s/h/e/k/f/c
@echo off
    setlocal enableextensions

    set "sourceRoot=%cd%"
    set "target=d:\test"

    for /r /d %%a in (*) do (
        set "bottom=1"
        for /d %%b in ("%%~fa\*") do set "bottom="
        if defined bottom (
            echo move "%%~fa" "%target%"
        )
    )

This will search the last directory in each sub branch of indicated source, independtly of name, and move (when the output to console is correct, remove the echo ) the subdirectory to the target folder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM