How to copy and rename subfolders under variable folders?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
machone
Posts: 31
Joined: 01 Oct 2012 18:36

How to copy and rename subfolders under variable folders?

#1 Post by machone » 01 Oct 2012 19:12

I'm trying to find a way to save some time in generating a quick secondary copy of some photoshop files I'm working with. Here is the structure I have:

toplevel\variable_folder_name\PSD\

so, for example:

toplevel\1234567890\PSD\
toplevel\0987654321\PSD\

What I need to end up with is:

toplevel\1234567890\PSD\
toplevel\1234567890\PSD_temp\
toplevel\0987654321\PSD\
toplevel\0987654321\PSD_temp\

Basically, I'd like to take the PSD folder, and its contents, and copy them to a new folder on the same level as the original PSD folder. Can anyone help with this or offer some guidance?

I was experimenting with this.. It seems to read all the files but doesn't actually copy them:

Code: Select all

for /d %%a in ("e:\0-TESTING\*") do @copy "%%~Fa\PSD\*" "%%~Fa\PSD_temp\" 2>NUL

machone
Posts: 31
Joined: 01 Oct 2012 18:36

Re: How to copy and rename subfolders under variable folders

#2 Post by machone » 01 Oct 2012 19:24

Actually, I think I got it, unless someone can point out potential issues I'm not foreseeing...

Code: Select all

for /d %%a in ("e:\0-TESTING\*") do @xcopy "%%~Fa\PSD\*" "%%~Fa\PSD_TEMP" /I 2>NUL


EDIT: Ah, well, one thing I've found so far is if the original PSD folder has its own subfolder with contents, those don't get copied. Not sure what I'm missing.

EDIT2: What I was missing was the /E to copy dirs and subdirs including empty ones, so:

Code: Select all

for /d %%a in ("e:\0-TESTING\*") do @xcopy "%%~Fa\PSD\*" "%%~Fa\PSD_2" /I /E 2>NUL

machone
Posts: 31
Joined: 01 Oct 2012 18:36

Re: How to copy and rename subfolders under variable folders

#3 Post by machone » 02 Oct 2012 04:43

Hmm.. does that offer any advantages over mine? You seem to be "hard-coding" into a batch file the folders in which it'll operate, whereas mine will travel the directory structure until its done.

In my real-world situation this needs to work on over 1600 project folders with more being added weekly. I don't see how your method would work, whereas mine is ready to go for as many folders as are thrown at it.. I'll admit I'm a batch scripting noobie though, so I could very well be missing something.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: How to copy and rename subfolders under variable folders

#4 Post by abc0502 » 02 Oct 2012 05:04

IS all the folders that will be renamed named "PSD"

Edit:
if so, try this, but test it first
IF a folder with this name "PSD_temp" exist (inside the folder with the number names) it will be re-write over it.
and all data inside it if these data has a similar name of the files that is being copied

Code: Select all

@Echo Off & Cls & Color 0A & Mode 50,10

Setlocal EnableDelayedExpansion
:: Set This Variable to Your Toplevel Folder
:: beware if there is folder with the same name "PSD_temp" it will RE-WRITE over it.
set "toplvl=%userprofile%\Desktop\toplevel\"
For /F "tokens=* delims=" %%A in ('Dir /B /S /A:D "%toplvl%"') Do (
   IF "%%~nA"=="PSD" (
      set "backdir=%%~sA"
      Echo Y|Xcopy "%%~sA" "!backdir:~0,-3!\PSD_temp" /E /I /H >nul )
)

Echo.&Echo   Done.
pause >nul
exit /b


Set the variable "toplvl" line 6, and run the batch TEST IT FIRST

machone
Posts: 31
Joined: 01 Oct 2012 18:36

Re: How to copy and rename subfolders under variable folders

#5 Post by machone » 02 Oct 2012 05:39

I'll give it a try this morning, thanks. But are there any advantages with yours over mine? I've tested this one, and it works:

Code: Select all

for /d %%a in ("e:\0-TESTING\*") do @xcopy "%%~Fa\PSD\*" "%%~Fa\PSD_2" /I /E 2>NUL

and it's only 1 line as opposed to your 11 lines...

(And yes, every folder has a PSD subfolder.)

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: How to copy and rename subfolders under variable folders

#6 Post by abc0502 » 02 Oct 2012 05:57

I just tested your code, it work the same, I don't what exact differences between yours and mine except the length :)
but use yours it less complicated

machone
Posts: 31
Joined: 01 Oct 2012 18:36

Re: How to copy and rename subfolders under variable folders

#7 Post by machone » 02 Oct 2012 06:08

:) Cool. Thanks for the example though. I can look through it and try to learn other ways of doing it.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: How to copy and rename subfolders under variable folders

#8 Post by abc0502 » 02 Oct 2012 06:11

machone wrote::) Cool. Thanks for the example though. I can look through it and try to learn other ways of doing it.

many ways can do the same thing but the performance is what can change from code to another.
so try the codes on many files and see how long it will take till it finish

come by later, some one here can post a better code

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

Re: How to copy and rename subfolders under variable folders

#9 Post by foxidrive » 02 Oct 2012 06:15

If the folders and files already exist and you are updating the folders, then using Robocopy is a good plan with the options to mirror the folder.
That doesn't recopy files that are already there.

It does seem that the OP is recreating from scratch every time though.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: How to copy and rename subfolders under variable folders

#10 Post by abc0502 » 02 Oct 2012 06:34

foxidrive wrote:If the folders and files already exist and you are updating the folders, then using Robocopy is a good plan with the options to mirror the folder.
That doesn't recopy files that are already there.

It does seem that the OP is recreating from scratch every time though.

He wan't to make a sort of backup copy of the all PSD folders in the top level folder and name this backup folders with the name "PSD_temp"

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

Re: How to copy and rename subfolders under variable folders

#11 Post by foxidrive » 02 Oct 2012 06:52

This should work too.

Code: Select all

@echo Off
pushd "e:\test\"
For /F "delims=" %%a in ( ' Dir /B /A:D ' ) do if exist "%%a\PSD\" Xcopy "%%a\PSD\*.*" "%%a\PSD_temp\" /S/H/E/K/F/C
popd



The OP's solution with "for /d" can fail with a bug, where long pathname elements exist in the foldernames.

machone
Posts: 31
Joined: 01 Oct 2012 18:36

Re: How to copy and rename subfolders under variable folders

#12 Post by machone » 02 Oct 2012 09:21

Ah, ok, that might be an issue in the longrun, since some of the paths are long (product names and series numbers). I plan to test it on a batch of 250 folders later today and see if it finishes without error. I also plan to test abc0502's method on the same sample set and see if it's noticeably faster or not.

To answer your question, no, the new PSD_2 or PSD_temp folder doesn't exist.. it's being created specifically for this process each time we need a new side-by-side copy to work from, so we don't ever touch our source.

Post Reply