Page 1 of 1

Batch file to rename while copying to subdir

Posted: 09 Apr 2014 13:31
by machone
I've got a batch file which was created with help from forum members here a while ago. The script travels recursively from a top level folder, and in each subfolder it finds files with specific extensions and copies them to a subdirectory called 'rsrc'. What I'm trying to do now is make this script accomplish several things in one.

Here is the script as of now:

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in ( ' dir "*.png" /b /s /a-d ' ) do (
echo processing "%%a"
set "name=%%~nxa"
pushd "%%~dpa"
copy /b /y "%%a" ".\rsrc\!name:~0!"
popd
)


Here's what I'd like to hopefully have the script accomplish in one shot:

1. Create the 'rsrc' subdir under each 1st-level folder. For example, if the script is running from TOPLEVEL\ then it should create:

TOPLEVEL\subdir1\rsrc\
TOPLEVEL\subdir2\rsrc\
TOPLEVEL\subdir2\randomfolder\ (this folder would not get a 'rsrc' subdir because it's 2 levels down)

I'm currently using a separate batch to accomplish this prior to running the script above, but it would be nice to fold it into the same script:

Code: Select all

@echo off
pushd .\
FOR /F "delims=" %%G in ('dir /ad /b') do mkdir "%%G\rsrc"
popd


2. Copy not only PNG files but JPG also, or whatever formats I need for that specific pass.

3. Rename the target files, while copying, from the starting extension to a dummy extension, to hide them from a graphics editor. For example, if the starting image is:

TOPLEVEL\subdir1\ImageFile.PNG
the resulting file should be:
TOPLEVEL\subdir1\rsrc\ImageFile.TEMP

I have all this running as separate scripts and I've documented my workflow so I can do this all pretty quickly, but it would be nice to fold it all into a single script if possible, if someone would be kind enough to help...

m1

Re: Batch file to rename while copying to subdir

Posted: 10 Apr 2014 06:06
by foxidrive
A solution is very simple - but the first script is supposed to find a rsrc folder in *every* folder that contains the images. Not just the first level.

How do you want to proceed here - or are there images only in first level folders?

A second question is that if you rename all images to .temp then apple.png and apple.jpg will overwrite each other if they both have .temp extensions. Or do you want apple.png.temp and apple.jpg.temp ?

Re: Batch file to rename while copying to subdir

Posted: 11 Apr 2014 12:25
by machone
Hmm, yes it would be safest if it only looked in top-level folders, i.e.

toplevel\ (script is run here)
toplevel\sublevel\ (images copied from here)
toplevel\sublevel\subsublevel\ (images ignored here)

Your second suggestion is a good one. The chances that two images with different extensions would exist is almost zero, but yes, that's a good suggestion. Better to be safe and plan ahead for the possibility so .jpg.temp and .png.temp is preferable.

Re: Batch file to rename while copying to subdir

Posted: 11 Apr 2014 23:18
by foxidrive
Test this in the toplevel folder.

Code: Select all

@echo off
for /d %%a in (*) do (
 echo processing "%%a"
   for %%b in ("%%a\*.png" "%%a\*.jpg") do (
     echo found "%%b"
     md "%%a\rsrc" 2>nul
     copy "%%~b" "%%a\rsrc\%%~nxb.temp" >nul
   )
)

Re: Batch file to rename while copying to subdir

Posted: 14 Apr 2014 18:34
by machone
Awesome, thank you, I'll try this out in the morning!