Copy folders with filename

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
amitananddey
Posts: 14
Joined: 16 Jan 2012 04:56

Copy folders with filename

#1 Post by amitananddey » 07 May 2012 22:45

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Copy folders with filename

#2 Post by !k » 08 May 2012 00:50

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!"
)

prash11
Posts: 21
Joined: 27 Apr 2012 01:38

Re: Copy folders with filename

#3 Post by prash11 » 08 May 2012 01:08

@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....!

amitananddey
Posts: 14
Joined: 16 Jan 2012 04:56

Re: Copy folders with filename

#4 Post by amitananddey » 08 May 2012 01:44

Thank u !k and prash11.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Copy folders with filename

#5 Post by Squashman » 08 May 2012 05:51

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.

prash11
Posts: 21
Joined: 27 Apr 2012 01:38

Re: Copy folders with filename

#6 Post by prash11 » 08 May 2012 06:25

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.....!

prash11
Posts: 21
Joined: 27 Apr 2012 01:38

Re: Copy folders with filename

#7 Post by prash11 » 08 May 2012 06:45

@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.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Copy folders with filename

#8 Post by Squashman » 08 May 2012 07:10

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.

prash11
Posts: 21
Joined: 27 Apr 2012 01:38

Re: Copy folders with filename

#9 Post by prash11 » 08 May 2012 07:28

hi Squashman,
yes you are right....!

amitananddey
Posts: 14
Joined: 16 Jan 2012 04:56

Re: Copy folders with filename

#10 Post by amitananddey » 17 May 2012 23:22

!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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy folders with filename

#11 Post by foxidrive » 18 May 2012 00:25

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
)

Post Reply