Batch to recursively add directory names to file names

Question:

I need a batch file (no VB please) to recurse through all subdirectories under a folder, find the subdirectory name, and PRE-pend that subdirectory name to every file in the subdirectory.  It is a little tricky, any ideas?

situation — the “PICS” folder has about 800 subdirectories under it — 01 up to 800 are the subdirectory names.  In each subdir, are files named 01.jpg to 25.jpg.  I want to concatenate all these thousands of files into a single directory — to do that, they have to first have the numerical name of their folder prepended to the file names, otherwise they will all overwrite.  Get the picture?  Any batch ideas appreciated.  Thx.

BTW — it is OK just to use the copy command, I have lots of space.  e.g. –

copy   G:\pics\01\09.jpg    G:\pics\01-09.jpg

see how the directory name 01 is prepended to the final file name?  Need something like that to recurse the entire tree and all files in all subdirs.

Solution:

Sure:

@echo off

setlocal

set rootDir=g:\pics
set targetDir=g:\ppics
set fileMask=*.jpg

pushd “%rootDir%”

for /f “tokens=*” %%a in (‘dir /b /ad 2^>NUL’) do call :P ROCESSDIR “%%a”

popd

goto :EOF

:P ROCESSDIR

pushd %1

for /f “tokens=*” %%a in (‘dir /b /a-d “%fileMask%” 2^>NUL’) do echo copy “%%~fa” “%targetDir%\%~n1-%%~nxa”

popd

Tags: · · · ·
digg delicious stumbleupon technorati Google live facebook Sphinn Mixx newsvine reddit yahoomyweb
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...