0

I require a windows batch script to recursively rename files and folders with the name of the parent folder and add a prefix/suffix.

I have 2 folders fol1 and fol2, with the following directory structure:

fol1
  ├───fol1.1
  │   └───text.txt
  ├───fol1.2
  │   └───text.txt
fol2
  ├───fol2.1
  └───fol2.2

I want the tree renamed as:

lon_fol1_par
  ├───lon_fol1.1_par
  │   └───fol1.1_text.txt
  ├───lon_fol1.2_par
  │   └───fol1.2_text.txt
lon_fol2_par
  ├───lon_fol2.1_par
  └───lon_fol2.2_par

The code that helped me renaming is as follows:

FOR /F "tokens=1 delims=" %%A IN ('DIR /B /S /A:D') DO (

   FOR /F "tokens=1 delims=" %%B IN ('DIR /B /A:-D "%%A"') DO (
    pushd %%A
    FOR %%I IN (.) DO RENAME "%%A\%%B" "<Name I want>"%%~xB
    popd ..
  )   
)  
abhi
  • 79
  • 1
  • 1
  • 10
  • 1
    Yes, it's possible; what have you tried so far? please remember that SO is not a coding service... – aschipfl Sep 14 '15 at 18:59
  • Do you only have .txt files inside the folders? – foxidrive Sep 14 '15 at 21:27
  • Very similar to what you need: http://stackoverflow.com/questions/8397674/windows-batch-file-looping-through-directories-to-process-files – Nate Barbettini Sep 14 '15 at 21:33
  • Yes @aschipfl, I have tried something. FOR /F "tokens=1 delims=" %%A IN ('DIR /B /S /A:D') DO ( FOR /F "tokens=1 delims=" %%B IN ('DIR /B /A:-D "%%A"') DO ( pushd %%A FOR %%I IN (.) DO RENAME "%%A\%%B" "%array[1]%_%%~nB_%%~nI_%array[2]%_%array[3]%%%~xB" popd .. ) ) – abhi Sep 15 '15 at 04:07
  • Sorry... am new to this. Please manage to read it carefully @aschipfl . Thanks :) – abhi Sep 15 '15 at 04:08
  • @foxidrive: Files can be of any type :) – abhi Sep 15 '15 at 04:10
  • @abhi You can edit your question to add the code properly, and also give accurate details of the task if you want accurate code that works in your situation. – foxidrive Sep 15 '15 at 04:39
  • @foxidrive: got it. done it. Thanks :) Now guide me – abhi Sep 15 '15 at 05:29
  • pls check this @foxidrive http://stackoverflow.com/questions/32578334/i-want-to-rename-files-without-repetition-using-windows-batch-scripting – abhi Sep 15 '15 at 05:30
  • @abhi Please be clear and edit your question. In the current example you are also not only renaming folder but files to his parent folder,., No need to create another thread for the same problem. – Paul Sep 15 '15 at 18:32

1 Answers1

1

Renaming files first before adding a prefix and a suffix to the folder names

Be aware: This script implies that there have not excluded directories renaming process that would only condition: the folder containing the files text.txt

If so, you have to do something else that are not in that script.

Note: Remove echo command in front of rename command to make it active. Also you can add PAUSE command one line after rename command to see what happens

@echo off
REM Renaming files first before adding a prefix and a suffix to the folder names

REM Be aware: This script implies that there have not excluded directories...
REM ...renaming process that would only condition: the folder containing the files text.txt
REM If so, you have to do something else that are not in that script.

REM Note: Remove echo command in front of rename command to make it active.

setlocal enabledelayedexpansion
FOR /F "tokens=* delims=" %%F in ('dir /s /b /a-d text.txt') DO (
    set "DIRPATH=%%~dpF"
    set "FILEPATH=%%~F"
    set "FILENAME=%%~nxF"
    IF "!DIRPATH:~-1!" EQU "\" (
        SET "DIRPATH=!DIRPATH:~0,-1!"
    )

    FOR %%G IN ("!DIRPATH!") DO (
        set "PICDIR=%%~nxG"
        echo:
        echo   renaming files
        echo   rename "!FILEPATH!" "!DIRPATH!\!PICDIR!_!FILENAME!"
    )
)
endlocal
REM Then renaming folders with prefix and suffix
set "prefix_=lon_"
set "_suffix=_par"
FOR /F "tokens=* delims=" %%D in ('dir /s /b /ad') do (
    echo   renaming folders
    echo   rename "%%~D" "%prefix_%%%~nD%_suffix%"
)

EXIT /B 0
Paul
  • 2,620
  • 2
  • 17
  • 27