Batch file to rename while copying to subdir

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Batch file to rename while copying to subdir

#1 Post by machone » 09 Apr 2014 13:31

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

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

Re: Batch file to rename while copying to subdir

#2 Post by foxidrive » 10 Apr 2014 06:06

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 ?

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

Re: Batch file to rename while copying to subdir

#3 Post by machone » 11 Apr 2014 12:25

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.

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

Re: Batch file to rename while copying to subdir

#4 Post by foxidrive » 11 Apr 2014 23:18

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
   )
)

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

Re: Batch file to rename while copying to subdir

#5 Post by machone » 14 Apr 2014 18:34

Awesome, thank you, I'll try this out in the morning!

Post Reply