Page 1 of 1

Copy folders with filename

Posted: 07 May 2012 22:45
by amitananddey
I have a folder structure like this
├───blue
──apple.png
──box.png
──cartoon_imp.png
├───green
──cat.png
──dog_imp.png
──fox

There is *_imp.png in every folder.I want to rename every folder with corresponding to *_imp.png filename inside it.
Like this

├───cartoon
──apple.png
──box.png
──cartoon_imp.png
├───dog
──cat.png
──dog_imp.png
──fox

Please help

Re: Copy folders with filename

Posted: 08 May 2012 00:50
by !k

Code: Select all

setlocal enabledelayedexpansion
for /f "delims=" %%f in ('dir /b/a-d/s *_imp.png') do (
   set "name=%%~nf"
   ren "%%~dpf." "!name:~0,-4!"
)

Re: Copy folders with filename

Posted: 08 May 2012 01:08
by prash11
@echo off

cd blue
FOR %%G IN (*_imp.png) DO (

echo file found...!

echo %%~nG > file.txt
FOR /F "delims=_" %%M IN (file.txt) DO (

echo %%M
cd..
mkdir %%M

move /y blue\*.png %%M\
echo y | del blue

)


)
rmdir blue


and make sure you run this program from outside blue folder.

suppose you have blue folder in this way..... D:\youR_folder\your_folder2\blue
then run your script from your_folder2
cheers....!

Re: Copy folders with filename

Posted: 08 May 2012 01:44
by amitananddey
Thank u !k and prash11.

Re: Copy folders with filename

Posted: 08 May 2012 05:51
by Squashman
prash11 wrote:@echo off

cd blue
FOR %%G IN (*_imp.png) DO (

echo file found...!

echo %%~nG > file.txt
FOR /F "delims=_" %%M IN (file.txt) DO (

echo %%M
cd..
mkdir %%M

move /y blue\*.png %%M\
echo y | del blue

)


)
rmdir blue


and make sure you run this program from outside blue folder.

suppose you have blue folder in this way..... D:\youR_folder\your_folder2\blue
then run your script from your_folder2
cheers....!


Where does this process the other folders with a *_imp.png file names in them? You only did the blue folder. And, if there are other files in the directory besides PNG files those will be deleted.

Re: Copy folders with filename

Posted: 08 May 2012 06:25
by prash11
replace this
move /y blue\*.png %%M\


with
move /y blue\* %%M\

done....!

and just replace blue with whatever folder u want.
cntrl+f then find blue replace with green.


done.....!

Re: Copy folders with filename

Posted: 08 May 2012 06:45
by prash11
@echo off


FOR %%G IN (*_imp.png) DO (

echo file found...!

echo %%~nG > file.txt
FOR /F "delims=_" %%M IN (file.txt) DO (

echo %%M

mkdir %%M
cd %%M
move /y ..\*.png
cd..
)

)



just put this bat in that folder in which *_imp.png is there.
and after that u will get all .png in that perticular folder.

Re: Copy folders with filename

Posted: 08 May 2012 07:10
by Squashman
prash11,
I think the point of the exercise is to process all the files and directories in one execution of the batch file. Your solution only processes one directory at a time. If you had a 100 folders like this would you want to copy your batch file into each folder or edit the batch file to change the directory name.

!K!'s solution is processing everything at once.

Re: Copy folders with filename

Posted: 08 May 2012 07:28
by prash11
hi Squashman,
yes you are right....!

Re: Copy folders with filename

Posted: 17 May 2012 23:22
by amitananddey
!k wrote:

Code: Select all

setlocal enabledelayedexpansion
for /f "delims=" %%f in ('dir /b/a-d/s *_imp.png') do (
   set "name=%%~nf"
   ren "%%~dpf." "!name:~0,-4!"
)


Hi !k,
There is a problem.Your code works fine if I copy the bat file and run it inside the folder.
But if I drag and drop the folder into the batch file, it changes the folder name from the entire drive.
Is it possible to use the above script so that I can drag and drop folders.
Thanks

Re: Copy folders with filename

Posted: 18 May 2012 00:25
by foxidrive
This is untested but should handle a single folder.

@echo off
setlocal enabledelayedexpansion
pushd "%~dp1" && (
for /f "delims=" %%f in ('dir /b/a-d/s *_imp.png') do (
set "name=%%~nf"
ren "%%~dpf." "!name:~0,-4!"
)
popd
)