Link to home
Start Free TrialLog in
Avatar of wasabi3689
wasabi3689Flag for United States of America

asked on

how to roll into all sub dirs

I have the following Windows script working fine. there is one issue I want to add.

I have root directory C:\Sample_PDF\, under the root, there are sub diredtories then the files are resided, then I  run the .exe, I want program automatically to go into each the sub dir to execute .exe., make the converted files still resided in its original file directories

So, how to modify this code?
Avatar of wasabi3689
wasabi3689
Flag of United States of America image

ASKER

Here is the batch file

@echo off
setlocal enabledelayedexpansion

set startTime=%time%

echo Start Time: %startTime%

set SourceDir=C:\Sample_PDF\
set TargetDir=C:\Sample_PDF\
set FNPattern=*.doc

if not exist "%SourceDir%\" echo %~nx0 Missing source folder "%SourceDir%"&exit /b 1
if not exist "%TargetDir%\" echo %~nx0 Missing target folder "%TargetDir%"&exit /b 1
set CurDir=%cd%
cd /d "%SourceDir%"
for %%a in ("%FNPattern%") do C:\Sample_PDF\OfficeToPDF.exe "%%~fa" "%TargetDir%\%%~na.pdf"
cd /d "%CurDir%"

pause

echo Finish Time: %time%

Open in new window

Avatar of Bill Prew
Bill Prew

If I understand correctly, this should do the job.  It uses a FOR /D to get the folders in the base directory and then processes the matching files in each of those.  Output goes to the same folder as the input.

@echo off
setlocal enabledelayedexpansion

set startTime=%time%

echo Start Time: %startTime%

set SourceDir=C:\Sample_PDF
set FNPattern=*.doc

if not exist "%SourceDir%\" (
  echo %~nx0 Missing source folder "%SourceDir%"
  exit /b 1
)

for /d %%A in ("%SourceDir%\*.*") do (
  for %%a in ("%%~A\%FNPattern%") do (
    C:\Sample_PDF\OfficeToPDF.exe "%%~a" "%%~dpna.pdf"
  )
)

pause

echo Finish Time: %time%

Open in new window

*EDIT* : Corrected BaseDir name to SourceDir.

~bp
Using Bill's code, remove the trailing \ from these. Then it should work.

set SourceDir=C:\Sample_PDF\
set TargetDir=C:\Sample_PDF\

Open in new window


becomes
set SourceDir=C:\Sample_PDF
set TargetDir=C:\Sample_PDF

Open in new window

I don't know the .doc file is not converted. When I run it, I don't see an error, just no output? how to debug it??
It looks like there is a bug. For example,

if my root is set to C:\Sample_PDF, but if I have more than 2 sub dirs like

C:\Sample_PDF\ABC\EFG\MMB\KLJ\file.doc

The program doesn't work

If my root is set to C:\Sample_PDF\ABC\EFG\MMB, it works.

I want to set the root then go through all sub dirs

Here is the code, how to fix this issue?

@echo off
setlocal enabledelayedexpansion

set startTime=%time%

echo Start Time: %startTime%

set BaseDir=C:\Sample_PDF\PDF

set FNPattern=*.doc

if not exist "%BaseDir%\" (
  echo %~nx0 Missing source folder "%BaseDir%"
  exit /b 1
)

for /d %%A in ("%BaseDir%\*.*") do (
  for %%a in ("%%~A\%FNPattern%") do (
    OfficeToPDF.exe "%%~a" "%%~dpna.pdf"
  )
)

echo Finish Time: %time%

pause

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A++++ solution