Copy folders with filename
Moderator: DosItHelp
-
- Posts: 14
- Joined: 16 Jan 2012 04:56
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
├───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
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
@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....!
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....!
-
- Posts: 14
- Joined: 16 Jan 2012 04:56
Re: Copy folders with filename
Thank u !k and prash11.
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.
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.....!
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
@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.
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
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.
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
hi Squashman,
yes you are right....!
yes you are right....!
-
- Posts: 14
- Joined: 16 Jan 2012 04:56
Re: Copy folders with filename
!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
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
)