View unanswered posts | View active topics It is currently 03 Sep 2014 01:02



Post new topic Reply to topic  [ 11 posts ] 
Copy folders with filename 
Author Message

Joined: 16 Jan 2012 04:56
Posts: 14
Post Copy folders with filename
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


07 May 2012 22:45
Profile
Expert

Joined: 17 Oct 2009 08:30
Posts: 378
Location: Russia
Post Re: Copy folders with filename
Code:
setlocal enabledelayedexpansion
for /f "delims=" %%f in ('dir /b/a-d/s *_imp.png') do (
   set "name=%%~nf"
   ren "%%~dpf." "!name:~0,-4!"
)


08 May 2012 00:50
Profile

Joined: 27 Apr 2012 01:38
Posts: 21
Post Re: Copy folders with filename
@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....!


08 May 2012 01:08
Profile

Joined: 16 Jan 2012 04:56
Posts: 14
Post Re: Copy folders with filename
Thank u !k and prash11.


08 May 2012 01:44
Profile

Joined: 23 Dec 2011 13:59
Posts: 1803
Post Re: Copy folders with filename
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.


08 May 2012 05:51
Profile

Joined: 27 Apr 2012 01:38
Posts: 21
Post Re: Copy folders with filename
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.....!


08 May 2012 06:25
Profile

Joined: 27 Apr 2012 01:38
Posts: 21
Post Re: Copy folders with filename
@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.


08 May 2012 06:45
Profile

Joined: 23 Dec 2011 13:59
Posts: 1803
Post Re: Copy folders with filename
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.


08 May 2012 07:10
Profile

Joined: 27 Apr 2012 01:38
Posts: 21
Post Re: Copy folders with filename
hi Squashman,
yes you are right....!


08 May 2012 07:28
Profile

Joined: 16 Jan 2012 04:56
Posts: 14
Post Re: Copy folders with filename
!k wrote:
Code:
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


17 May 2012 23:22
Profile

Joined: 10 Feb 2012 02:20
Posts: 4122
Post Re: Copy folders with filename
This is untested but should handle a single folder.

Quote:
@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
)


18 May 2012 00:25
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 


Who is online

Users browsing this forum: Yahoo [Bot] and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.