Renaming files by folder hierarchy

I have many files with the following structure:

1969/ar/1.jpg 1969/ar/2.jpg 1969/he/1.jpg 1969/he/2.jpg 1969/en/1.jpg 1969/en/2.jpg 1970/ar/1.jpg 

etc...

I want to rename all of them with one command into one directory, while their names reflect the original location of the folder.

 1969_ar_1.jpg 1969_ar_2.jpg 1969_he_1.jpg 1969_he_2.jpg 1969_en_1.jpg 1969_en_2.jpg 1970_ar_1.jpg 

etc...

Is it possible to do this with a single command or batch file?

Thanks!

+4
source share
3 answers

You can do this to move files to the base folder using this command line:

 for /R %a in (*) do @set f=%a& set f=!f:%cd%\=!& move "%a" !f:\=_! 

Run it from the folder containing the folders 1969, 1970 .... IMPORTANT !: The delayed extension must be active for this line to work, so you must first activate it by running cmd.exe using the / V switch as follows: cmd /V

For instance:

 >xcopy test backup /s test\1969\ar\1.jpg test\1969\ar\2.jpg test\1969\en\1.jpg test\1969\en\2.jpg test\1969\he\1.jpg test\1969\he\2.jpg test\1970\ar\1.jpg 7 File(s) copied >cd test >dir /B 1969 1970 >for /R %a in (*) do @set f=%a& set f=!f:%cd%\=!& move "%a" !f:\=_! >dir /B 1969 1969_ar_1.jpg 1969_ar_2.jpg 1969_en_1.jpg 1969_en_2.jpg 1969_he_1.jpg 1969_he_2.jpg 1970 1970_ar_1.jpg 

Change the line so that the files are moved to another folder:

 for /R %a in (*) do @set f=%a& set f=!f:%cd%\=!& move "%a" "\other\folder\!f:\=_!" 

Or through this batch file:

 @echo off setlocal EnableDelayedExpansion for /R %%a in (*) do set f=%%a& set f=!f:%cd%\=!& move "%%a" "\other\folder\!f:\=_!" 
+4
source

Run this from the base of the tree containing all the * .jpg files. Change the destination folder to where you want the files:

First check it on some samples.

 @echo off for /f "delims=" %%z in ('dir "*.jpg" /b /s /ad ') do ( for %%a in ("%%~dpz%\.") do ( for %%b in ("%%~dpa\.") do ( ren "%%z" "%%~nxb_%%~nxa_%%~nxz" move "%%~dpz\%%~nxb_%%~nxa_%%~nxz" "c:\target\folder" ) ) ) pause 
+3
source

try this (look at the output and delete the word echo until move , if ok):

 @echo off &setlocal for /d %%i in (19* 20*) do ( cmd /c "for /r "%%i" %%j in (*.jpg) do @for %%k in ("%%~dpj.") do @echo move "%%~j" "%%i_%%~nk_%%~nxj"" ) 
+3
source

Source: https://habr.com/ru/post/1486470/


All Articles