Rename file based on folder name and copy to:

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
LordNecro
Posts: 14
Joined: 18 Apr 2012 03:03

Rename file based on folder name and copy to:

#1 Post by LordNecro » 18 Apr 2012 03:09

Hey guys, ive been spending hours trying to write a script:

This script will sit in my hard drive, next to my movies folder(which has hundreds of movie folders inside of it), each movie folder is named with YAMMM, my multimedia software and provided with a .xml file with the movies information.

I want this script to list all movie folders, then copy the .xml files out of each movie folder and rename all the .xml files based on the folder name and place it in a different folder on my C: drive.

any way this is possible?

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

Re: Rename file based on folder name and copy to:

#2 Post by foxidrive » 18 Apr 2012 04:04

Create "c:\folder\" first.

Run this in the movie folder - it will echo the copy commands to the screen.
If that's what you want then remove the 'echo' statement.

Code: Select all

@echo off
for /f "delims=" %%a in ('dir *.xml /b /s') do (
for /f "delims=" %%b in ("%%~dpa\.") do (
echo copy /b "%%a" "c:\folder\%%~nxb.xml"
)
)
pause

LordNecro
Posts: 14
Joined: 18 Apr 2012 03:03

Re: Rename file based on folder name and copy to:

#3 Post by LordNecro » 18 Apr 2012 04:15

from the echo statement, it appears to do what I want, but it is not creating the copies in C:\folder\

any ideas?

thanks alot too man, its awesome how fast and accurate you were in responding.

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

Re: Rename file based on folder name and copy to:

#4 Post by foxidrive » 18 Apr 2012 04:22

Is the echo statement still there?

LordNecro
Posts: 14
Joined: 18 Apr 2012 03:03

Re: Rename file based on folder name and copy to:

#5 Post by LordNecro » 18 Apr 2012 04:24

yeah, i left it in to see what it displays and it shows.
    copy "J:\Movies\Venom (2005)\mymovies.xml" "C:\folder\Venom (2005).xml"
    copy "J:\Movies\Warrior's Way, The (2010)\mymovies.xml" "C:\folder\Warrior's Way, The (2010).xml"
    copy "J:\Movies\Weekend at Bernie's (1989)\mymovies.xml" "C:\folder\Weekend at Bernie's (1989).xml"
    copy "J:\Movies\Zombie Apocalypse (2011)\mymovies.xml" "C:\folder\Zombie Apocalypse (2011).xml"

which is absolutely perfect, except it wont copy the file to C:\folder\

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

Re: Rename file based on folder name and copy to:

#6 Post by foxidrive » 18 Apr 2012 04:27

Read the instructions in the first post with the code :)

LordNecro
Posts: 14
Joined: 18 Apr 2012 03:03

Re: Rename file based on folder name and copy to:

#7 Post by LordNecro » 18 Apr 2012 04:29

yup, i realized that just then before you replied. me slaps forehead. :oops:

LordNecro
Posts: 14
Joined: 18 Apr 2012 03:03

Re: Rename file based on folder name and copy to:

#8 Post by LordNecro » 18 Apr 2012 04:33

Thank you so much foxidrive, life saver.

LordNecro
Posts: 14
Joined: 18 Apr 2012 03:03

Re: Rename file based on folder name and copy to:

#9 Post by LordNecro » 18 Apr 2012 05:21

So I tried adopting this script to work on the .jpg files that are in each folder and yet it doesnt pick them all up.

Code: Select all

@echo off
for /f "delims=" %%a in ('dir folder.JPG /b /s') do (
for /f "delims=" %%b in ("%%~dpa\.") do (
echo copy "%%a" "C:\xampp\htdocs\project-T\lists\movies\%%~nxb.JPG"
)
)
pause


as you can see i remove the *.jpg and used folder.jpg as there is a folder.jpg and a folder.original.jpg in every folder.

any idea why it isnt working?

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

Re: Rename file based on folder name and copy to:

#10 Post by foxidrive » 18 Apr 2012 05:24

LordNecro wrote:So I tried adopting this script to work on the .jpg files that are in each folder and yet it doesnt pick them all up.

Code: Select all

@echo off
for /f "delims=" %%a in ('dir folder.JPG /b /s') do (
for /f "delims=" %%b in ("%%~dpa\.") do (
echo copy "%%a" "C:\xampp\htdocs\project-T\lists\movies\%%~nxb.JPG"
)
)
pause


as you can see i remove the *.jpg and used folder.jpg as there is a folder.jpg and a folder.original.jpg in every folder.

any idea why it isnt working?


Give me an example of a path where it doesn't work, please.

It should work fine for every file named "folder.jpg"

The copy command should have a /b switch in it too.

LordNecro
Posts: 14
Joined: 18 Apr 2012 03:03

Re: Rename file based on folder name and copy to:

#11 Post by LordNecro » 18 Apr 2012 05:38

I've discovered it, all of the folder.jpg's are marked as hidden files, how would I go about ignoring such a property?

LordNecro
Posts: 14
Joined: 18 Apr 2012 03:03

Re: Rename file based on folder name and copy to:

#12 Post by LordNecro » 18 Apr 2012 05:39

I've discovered it, all of the folder.jpg's are marked as hidden files, how would I go about ignoring such a property?

or possibly a script to make all movie folders un/hidden recurring to all files

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

Re: Rename file based on folder name and copy to:

#13 Post by foxidrive » 18 Apr 2012 05:45

If it's just a hidden attribute then this should handle that: (untested)

Code: Select all

@echo off
for /f "delims=" %%a in ('dir folder.JPG /b /s') do (
for /f "delims=" %%b in ("%%~dpa\.") do (
attrib -h "%%a"
copy /b "%%a" "C:\xampp\htdocs\project-T\lists\movies\%%~nxb.JPG"
attrib +h "%%a"
)
)
pause

LordNecro
Posts: 14
Joined: 18 Apr 2012 03:03

Re: Rename file based on folder name and copy to:

#14 Post by LordNecro » 18 Apr 2012 05:56

i'm getting a file not found, as it cannot find the folder.jpg

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

Re: Rename file based on folder name and copy to:

#15 Post by foxidrive » 18 Apr 2012 06:46

Maybe the folder.jpg files also have a system attribute set.

open a folder with one of the JPG files and type

Code: Select all

attrib folder.jpg


It will show something like the bottom and may have SH in it.

c:\>attrib cmldr
A SHR C:\cmldr



If it does then try using this:


Code: Select all

@echo off
for /f "delims=" %%a in ('dir folder.JPG /b /s') do (
for /f "delims=" %%b in ("%%~dpa\.") do (
attrib -h -s "%%a"
copy /b "%%a" "C:\xampp\htdocs\project-T\lists\movies\%%~nxb.JPG"
attrib +h +s "%%a"
)
)
pause

Post Reply