views:

994

answers:

5

I'm trying to recurse through my music directory and copy every file called folder.jpg to a file in the same directory called cover.jpg.

I've tried variations of suggestions in this question such as this:

for /r %i in (folder.jpg) do copy %i cover.jpg

Resulting in "The system cannot find the file specified."

How can solve this problem?

Edit

Here's what I ended up going with:

for /r %i in (folder.jpg) do copy "%i" "%~picover.jpg"
+1  A: 

You probably don't have any files in your music folder called folder.jpg so it fails right?

I tried this in mymusic folder with a dummy file called folder.jpg and it copied it normally. ;)

Edit:

Kishi is right, you are missing the double quotes on the second %i

Konstantinos
Actually you were right at one stage :P I had previously recursively renamed all folder.jpg files to cover.jpg.
Kenny
+1  A: 

You are missing double-quotes on the copy command.

The %i variable will be holding the full-path to the file -- which may contain spaces. Try using:

for /r %i in (folder.jpg) do copy "%i" cover.jpg
MrKishi
Yeah I did miss the quotes actually, thanks. The next problem was that it would always copy to the directory from where I ran the command, eg. e:\Music
Kenny
A: 

You could just use xcopy with the /s flag...

EDIT: My bad - didn't read the question properly. Xcopy with /s will help when just copying the files to a fixed destination.

Prashast
+2  A: 

Try this:

for /f "usebackq delims==" %I in (`dir /b /s ^| findstr folder.jpg`) do copy "%I" "%~pIcover.jpg"

Decoder Ring:

usebackq :: run the command in the backquotes and use the output as the input for the loop
delims== :: use the equal sign as a delimeter. Really you could use any character that isn't valid in a file name
dir /b /s :: do a recursive directory listing only outputting the bare file names
^| :: ^ escapes the pipe character, the pipe - well pipes the output from the first command to the second
findstr :: searches the input for matching lines, and only outputs them
%~pI :: the tilde p instructs the variable expansion to only output the path rather than full file name + path. Note, this includes a trailing \

I hope this helps!

Goyuix
That's pretty intense command line action. It did the trick perfectly thanks. Also the Decoder Ring was great, especially the part about %~pI. I found that for /r %i in (folder.jpg) do copy "%i" "%~picover.jpg"also worked.
Kenny
This is awesome...
Tracker1
+1  A: 

PowerShell must replace CMD. It is inevitable and righteous. And it's my job to help it along...

gci -r . folder.jpg | % { copy $_.FullName ([IO.Path]::Combine( $_.Directory.FullName, "cover.jpg" )) }
Jay Bazuzi
Does Stack Overflow pull out underscores...? Should this be $_.FullName...
Timothy Lee Russell
SO's formatting of PowerShell code blocks doesn't work very well, as the syntax isn't very C-like. So I usually use <PRE> for PowerShell. In this case, it decided that the two '_' should make a span italic, even in <PRE>. So I made the compromise and switched it to a code block.
Jay Bazuzi