简体   繁体   中英

batch file to create new subfolder in folder, move file to newly created subfolder, for all folders in directory

I am needing help to move files in my library. I've searched and read for hours and can't find the proper solution, if any.

My directory looks like this

  • D:Music\\
    • D:Music\\Name - Title\\
    • D:Music\\Name - Title\\xxxx.jpg
    • D:Music\\Name - Title\\xxxx-type.jpg
    • D:Music\\Name - Title\\xxxx.xml

There are thousands of these entries in D:Music. Folder names 'Name - Title' are all different.

I want to move xxxx-type.jpg to new folder named 'artwork' in the 'Name - Title' folder. The files I want to move all end in '-type.jpg'.

The new directory would look like this:

  • D:Music\\
    • D:Music\\Name - Title\\
    • D:Music\\Name - Title\\xxxx.jpg
    • D:Music\\Name - Title\\xxxx.xml
    • D:Music\\Name - Title\\artwork
      • D:Music\\Name - Title\\artwork\\xxxx-type.jpg

I have tried this:

for /d %%a in ("D:Music\Name - Title\*") do mkdir "%%~fa\artwork" 2>nul  

This creates new folders in 'Name Title' folders. But obviously won't move the files.

I also tried this to create the folders and move the files:

@echo off
    for /f "delims=" %%a D:Music\Name - Title\ ('dir /s/b/a-d *.* ^| find /i "*-type.jpg"') do (
    if not exist "%%~dpaartwork" md "%%~dpaartwork"
    move "%%~fa" "%%~dpaartwork")

The above does nothing. Any help is appreciated.

Thanks

Try this

@echo off& SetLocal
for /f "delims=" %%a in ('dir /ad /b "D:\Music\"') do (
    cd /d "%%~fa"
    if exist *-type.jpg (
        if not exist artwork md artwork
        move *-type.jpg artwork
    )
)

This will go through all folders directly in D:\\Music and if there are some *-type.jpg files in that folder, it will move them into subfolder artwork (which will be created if it does not exist).

If you have folders with *-type.jpg files also in subfolders, you can use this

@echo off& SetLocal
for /f "delims=" %%a in ('dir /ad /b /s "D:\Music\"') do (
    cd /d "%%~fa"
    if /i not "%%~na"=="artwork" if exist *-type.jpg (
        if not exist artwork md artwork
        move *-type.jpg artwork
    )
)

This should work:

@echo off

for /R D:\Music %%F in (*-type.jpg) do (
   if not exist "%%~pFartwork" md "%%~pFartwork"
   move "%%F" "%%~pFartwork"
)

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