DosTips.com

A Forum all about DOS Batch
It is currently 26 Jul 2016 06:13

All times are UTC-06:00




Post new topic  Reply to topic  [ 11 posts ] 
Author Message
PostPosted: 07 May 2012 22:45 
Offline

Joined: 16 Jan 2012 04:56
Posts: 14
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


Top
   
PostPosted: 08 May 2012 00:50 
Offline
Expert

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


Top
   
PostPosted: 08 May 2012 01:08 
Offline

Joined: 27 Apr 2012 01:38
Posts: 21
@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....!


Top
   
PostPosted: 08 May 2012 01:44 
Offline

Joined: 16 Jan 2012 04:56
Posts: 14
Thank u !k and prash11.


Top
   
PostPosted: 08 May 2012 05:51 
Offline
Expert

Joined: 23 Dec 2011 13:59
Posts: 3140
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.


Top
   
PostPosted: 08 May 2012 06:25 
Offline

Joined: 27 Apr 2012 01:38
Posts: 21
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.....!


Top
   
PostPosted: 08 May 2012 06:45 
Offline

Joined: 27 Apr 2012 01:38
Posts: 21
@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.


Top
   
PostPosted: 08 May 2012 07:10 
Offline
Expert

Joined: 23 Dec 2011 13:59
Posts: 3140
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.


Top
   
PostPosted: 08 May 2012 07:28 
Offline

Joined: 27 Apr 2012 01:38
Posts: 21
hi Squashman,
yes you are right....!


Top
   
PostPosted: 17 May 2012 23:22 
Offline

Joined: 16 Jan 2012 04:56
Posts: 14
!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


Top
   
PostPosted: 18 May 2012 00:25 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 5799
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
)


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 11 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: Aacini, Bing [Bot], Yahoo [Bot] and 14 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® Forum Software © phpBB Limited