skip to Main Content

I know there exist quite a lot of script out there to do such tasks, but I couldn’t find any that can do what I am specifically searching for…

I am looking for a script that could read all files I have in a folder, separate all “_” and put them into subfolders.

The structure of the filenames look like this:

Clothes_Man_Citizen_Pants.png
Clothes_Man_Citizen_Top.png

The script would then create subfolders with this structure:

Clothes/Man/Citizen/Clothes_Man_Citizen_Pants.png
Clothes/Man/Citizen/Clothes_Man_Citizen_Top.png

I am asking this because I couldn’t find any photoshop script to export nested groups in photoshop… So I’m exporting all the layers into a folder and would like to create the nested folder structure with a batch or something like that.

Thanks for your answers !

EDIT:

In pseudo-code it would look like this:

InitialName = Clothes_Man_Citizen_Pants.png;
TempName = Clothes_Man_Citizen_Pants.png;

While (There is an underscore in TempName) {
    -> Read name until underscore
    -> Create folder with name
    -> Get into created folder
    -> Remove part from TempName
    -> Repeat
}

When there are no underscores anymore in TempName paste the file with InitialName in the last created folder.

2

Answers


  1. @echo off
        setlocal enableextensions disabledelayedexpansion
    
        for %%a in (*.png) do (
            rem Retrieve file name and extension
            set "fileName=%%~nxa"
    
            rem Prepare the folder from filename
            rem Filenames not splitted by underscores will be skipped
            setlocal enabledelayedexpansion 
            for %%b in ("::!fileName:_=!") do endlocal & if not "%%~pb"=="" %"% ( set "folder=%%~pb" ) else ( set "folder=" ) 
    
            rem If we have a target folder, ...
            if defined folder (
                rem We need a mask to move all the files in a folder in one command
    
                rem First remove leading and trailing backslashes
                setlocal enabledelayedexpansion 
                for %%b in ("!folder:~1,-1!") do endlocal & set "mask=%%~b"
    
                rem Now replace backslashes with underscores and add wildcard and extension
                setlocal enabledelayedexpansion 
                for %%b in ("!mask:=_!") do endlocal & set "mask=%%~b_*.png"
    
                rem Create the target folder and move the files 
                setlocal enabledelayedexpansion 
                if not exist ".!folder!" md ".!folder!"
                move /y "!mask!" ".!folder!" 
                endlocal 
            )
        )
    

    This takes the file name, replaces the underscores into backslashes and uses the generated string as a folder hierarchy to do all the operations.

    The setlocal / endlocal are needed to handle the posibility of exclamations (!) in the name/path of files. If it will never be the case, delayed expansion (needed in the code) can be enabled from the start and the previous code can be reduced to

    @echo off
        setlocal enableextensions enabledelayedexpansion
    
        for %%a in (*.png) do (
            set "fileName=%%~nxa"
            for %%b in ("::!fileName:_=!") do if not "%%~pb"=="" %"% ( set "folder=%%~pb" ) else ( set "folder=" )
            if defined folder (
                set "mask=!folder:~1,-1!"
                set "mask=!mask:=_!_*.png"
                if not exist ".!folder!" md ".!folder!"
                move /y "!mask!" ".!folder!" 
            )
        )
    

    note: in both codes you can find %"%, that is not needed. It is included only to correct the syntax hightlighting that fails because the "" in the code.

    Login or Signup to reply.
  2. Try this:

    EDIT: The original code had a bug. It is fixed now…

    @echo off
    setlocal EnableExtensions EnableDelayedExpansion
    
    for %%a in (*.png) do (
       set "filename=%%a"
       for %%b in (!filename:_= !) do set "lastPart=%%b"
       for %%b in (!lastPart!) do set "filename=!filename:_%%b=!"
       md "!filename:_=!" 2> NUL
       move "%%a" "!filename:_=!"
    )
    

    This method will fail if the file names may include spaces, comma or semicolon. It also remove exclamation marks.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search