Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch file to make folders with part of folder name and then copy files  (Read 3070 times)

0 Members and 1 Guest are viewing this topic.

Azure

    Topic Starter


    Newbie

    • Experience: Beginner
    • OS: Windows 10
    Hello!

    I am trying to organize a lot of folders contained within the same folder, most of them follow this standard when it comes to the folder name:

    "(code) XXX NAME (Source) XXX"
    //XXX represents random text that may be present.
    // The first parenthesis is not always present either.

    So, for example, I have a folder named "(C323) Fox (Carnivore)" within my folder "Animals" and need for the batch to take the contents within the last parenthesis and make a folder with that name (if it doesnt exist already) and move the orignal folder "(C323) Fox (Carnivore)" to the new folder "Carnivore".

    I have tried looking on stackoverflow and even asking myself, since all the similar things i find are for file names instead of folder names, I cant make it work.

    I noticed what I was asking is very similar to topic=152980.0 posted by doshisahil95 on this very forum, just that its a folder name and the folder name would be that of the parenthesis.

    Is this at all doable?

    I include my failure at making my own code, just for some laughs:
    Quote
    @echo off
    setlocal enabledelayedexpansion
    for %%A in (*) do (
       echo directory found  %%A
       for /D "delims=" %%B in ("%%A") do set fname=%%~nB
       for /D "delims=" %%C in ("%%A") do set fextn=%%~xC
       for /D "tokens=1* delims=()" %%D in ("!fname!") do set folname=%%D
       echo folder name !folname!
       if not exist "!folname!" (
          echo Folder !folname! does not exist, creating
          md "!folname!"
       ) else (
          echo Folder !folname! exists
       )
       echo Moving file %%A to folder !folname!
       move "%%A" "!folname!"
       )
    echo Finished
    pause
    I have some other attempts based on other posts i've found, if they help let me know!

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: Batch file to make folders with part of folder name and then copy files
    « Reply #1 on: January 03, 2018, 02:42:28 PM »
    This is the example I tested with based on your description.
    Code: [Select]
    C:\Nature>dir /ad /s /b
    C:\Nature\(C323) Fox (Carnivore)
    C:\Nature\C123) Wolf (Carnivore)
    Using this code.
    Code: [Select]
    @echo off
    FOR /D %%G IN (*) DO (
    FOR /F "tokens=3 delims=()" %%H IN ("%%~G") DO (
    MD "%%~H" 2>nul
    move "%%~G" "%%~H"
    )
    )
    Running the batch file.
    Code: [Select]
    C:\Nature>FolderMove.bat
            1 dir(s) moved.
            1 dir(s) moved.

    C:\Nature>dir /ad /s /b
    C:\Nature\Carnivore
    C:\Nature\Carnivore\(C323) Fox (Carnivore)
    C:\Nature\Carnivore\C123) Wolf (Carnivore)

    C:\Nature>