?
Solved

Windows 7: Batch-Operation: Recursive File Processing?

Posted on 2010-04-04
13
Medium Priority
?
627 Views
Last Modified: 2012-05-09
Hi,
I have the following problem:
Some 1000 files that need to be processed by a command line batch.
Files are organized in a large directory tree structure.
The idea is to start a batch file (.bat) which will start at root level, looking for a certain file type (*.dng), take these files, process them (extract JPG), store output (JPG) to the same folder location and loop through all directories down to the last file doing the same thing (recursive processing).
Is there a batch script (DOS) available?
Thanks.
0
Comment
Question by:go4java
  • 9
  • 4
13 Comments
 
LVL 16

Expert Comment

by:t0t0
ID: 29664947
yes! you need the following skeleton batch code and insert the line that actually processes your files (whatever that may be).


@echo off
setlocal enabledelayedexpansion

for /r "c:\" %%a in (*.dng) do (
   set filename=%%~nxa
   set filepath=%%~dpa
   ::
   :: This is where your code goes which processes the file held in the variable !filename!
   :: The path of the file is held in the variable !filepath!
   ::
)

0
 
LVL 16

Expert Comment

by:t0t0
ID: 29665152
Incidentally, where I have specified '"c:\"' in the above code, replace that with the root folder of your directory structure where all your DNG files are.

So, if all your DNG files are rooted off C:\DNGFILES\ then you need to code the FOR line like this:

   for /r "c:dngfiles\" %%a in (*.dng) do (
0
 

Author Comment

by:go4java
ID: 29679328
...I get the message "system cannot find the drive"...
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 16

Expert Comment

by:t0t0
ID: 29683450
interesting..... please hold on a mo....
0
 

Author Comment

by:go4java
ID: 29689222
...just to add the ACTION:

Each hit of *.dng should convert to *.jpg (and delete the original *.dng):

exiftool -copy1:previewimage -b input-image.dng > output-image.jpg

BR
0
 
LVL 16

Expert Comment

by:t0t0
ID: 29690145
Please look at the following skeleton code carefully.

I have included som e sample code so you can see the variable names in action.

Also, if you don't want to use the following method:

   "!filepath!\!filename!"

then you can simply CD into the folder first, then just use the filename etc as in the following example:

   cd "!filepath!"
   
   if not exist "new folder" (
      md "new folder"
   )

   copy "!filename!" "new folder"

as you can see, CD-ing into the folder makes a huge difference.

NOTE: Don't forget to change the '"c:\test\*.DNG"' in the FOR-line to suit your own needs.




@echo off
setlocal enabledelayedexpansion

for /f "tokens=*" %%a in ('dir /a-d /b /s "c:\test\*.DNG"') do (
   set filename=%%~nxa
   set filepath=%%~dpa
   set filepath=!filepath:~0,-1!

   rem
   rem This is where your code goes which processes the file held in the variable FILENAME
   rem The path of the file is held in the variable FILEPATH
   rem
   
   rem
   rem Below is some example code
   rem
   rem Bear in mind I have removed the trailing backslash character off FILEPATH
   rem as this makes it easier to use FILEPATH - see below
   rem
   
   rem
   rem Notice the use of the backslash character
   rem NOTICE the use of double-quotation marks around filenames and paths
   rem
   
   echo.
   echo Found file: !filename!
   echo In path: !filepath!
   echo.
   
   if not exist "!filepath!\NEW FOLDER" (
      echo Creating !filepath!\NEW FOLDER
      md "!filepath!\NEW FOLDER"
   )

   echo Copying !filename! to !filepath!\NEW FOLDER
   copy /y "!filepath!\!filename!" "!filepath!\new folder" >nul
)
exit /b
0
 
LVL 16

Expert Comment

by:t0t0
ID: 29690435
Reposted in the Code snippet box

.

@echo off
setlocal enabledelayedexpansion

for /f "tokens=*" %%a in ('dir /a-d /b /s "c:\test\*.DNG"') do (
   set filename=%%~nxa
   set filepath=%%~dpa
   set filepath=!filepath:~0,-1!

   rem
   rem This is where your code goes which processes the file
   rem held in the variable FILENAME
   rem
   rem The path of the file is held in the variable FILEPATH
   rem
   
   rem
   rem Below is some example code
   rem
   rem Bear in mind I have removed the trailing backslash
   rem character off FILEPATH as this makes it easier
   rem to use FILEPATH - see below
   rem
   
   rem
   rem Notice the use of the backslash character
   rem NOTICE the use of double-quotation marks around 
   rem filenames and paths
   rem
   
   echo.
   echo Found file: !filename!
   echo In path: !filepath!
   echo.
   
   if not exist "!filepath!\NEW FOLDER" (
      echo Creating !filepath!\NEW FOLDER
      md "!filepath!\NEW FOLDER"
   )

   echo Copying !filename! to !filepath!\NEW FOLDER
   copy /y "!filepath!\!filename!" "!filepath!\new folder" >nul
)

exit /b

Open in new window

0
 
LVL 16

Accepted Solution

by:
t0t0 earned 2000 total points
ID: 29691132
try this:


@echo off

set rootfolder=c:\dngfiles

for /f "tokens=*" %%a in ('dir /a-d /b /s "%rootfolder%\*.DNG"') do (
   exiftool -copy1:previewimage -b "%%~dpnxa" > "%%~dpna.jpg"
)
0
 

Author Comment

by:go4java
ID: 29691905
Thank you, I'll try.

Q: What is the meaning of the code:

"  if not exist "!filepath!\NEW FOLDER" (
      echo Creating !filepath!\NEW FOLDER
      md "!filepath!\NEW FOLDER"
   )

   echo Copying !filename! to !filepath!\NEW FOLDER
   copy /y "!filepath!\!filename!" "!filepath!\new folder" >nul"

Each level of directory structure will have 0 to n DNG files, so what's the reason behind creating new folders?

BR
0
 
LVL 16

Expert Comment

by:t0t0
ID: 29693982
i know...

i was just showing you some example code....on how to use the variables
0
 
LVL 16

Expert Comment

by:t0t0
ID: 29694029
i have to nip out now... will not be able to respond for several hours
0
 

Author Comment

by:go4java
ID: 29773350
SOLUTION:

Step 1 - Collect all and full DNG paths and write list to test.txt

dir /b /s *.dng > c:\test.txt

Schritt 2 - Loop test.txt and extract high-res JPEG images from raw DNG files (using fantastic tool dcraw.exe)

FOR /f "usebackq delims==" %%a IN (c:\test.txt) DO dcraw.exe -e "%%a"

Your loop will probably achieve the same thing, so I'll assign the points right know...

Thanks and best regards
0
 
LVL 16

Expert Comment

by:t0t0
ID: 29777196
Thank you.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
This Micro Tutorial will teach you how to the overview of Microsoft Security Essentials. This is a free anti-virus software that guards your PC against viruses, spyware, worms, and other malicious software. This will be demonstrated using Windows…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
Suggested Courses

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question