What I'm trying to do is copy a couple hundred images scattered throughout a directory tree and copy them all to another, single folder. I've found out how to do that with 'for /r %i in (*.jpg) ', but my main issue is that the files are all named using the same conventions. Sample:
Parent_Folder
001
file1.jpg
file2.jpg
002
file1.jpg
file2.jpg
The list continues as such. What I'm aiming to do is create this:
Dest_Folder
001_file1.jpg
001_file2.jpg
002_file1.jpg
002_file2.jpg
where the file's parent directory's name is added to the front of the files (so as to keep them listed in the same order). Not even individually going through every folder and using the "copy & rename" option will work since that will list all files named "file1.jpg" one after the other, "file2.jpg", and so on.
If there's any way to do this in a batch file, that would be much preferred, but a VBScript wouldn't be a bad choice either.
Thanks for any help!
EDIT
Magoo has given the perfect answer for my issue, but it seems his script only works if the source & destination paths have no spaces in them, despite the double quotes. I tried using this same script on another image collection, but with spaces in the paths, and I'm given an error saying the '[word after the first space in the path] was not expected at this time'. It was simple enough to just rename the folders, perform the copy, the rename them back, but is there a workaround for this?
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=c:\sourcedir"
SET "destdir=c:\destdir"
FOR /r "%sourcedir%" %%a IN (*.jpg) DO (
SET "newname=%%a"
SET "newname=!newname:*%sourcedir%\=!"
ECHO COPY /b "%%a" "%destdir%\!newname:\=_!"
)
GOTO :EOF
Grab each filename, remove the source directory name and a \
, then copy the file to that name, changing each \
to _
.
The required commands are merely ECHO
ed for testing purposes. After you've verified that the commands are correct, change ECHO COPY
to COPY
to actually copy the files.
Edit : fixed to allow spaces in sourcedir
(simply quote after the /r
in the for
)