Batch File

Dell / XPS
January 20, 2009 at 09:56:14
Specs: Windows XP, core 2 4 gig
Help!
I need a batch file that will go thru a directory find matching file names, there are pdf and txt files(ex 586159_0.PDF , I090112.586159_0.idx.txt) in this case the 586159_0 match. Move the matching files to directorys d:\image\001 002 003....
The file names are always in the same format.

Thank you for any advice.
Moshef


See More: Batch File

Report •


#1
January 20, 2009 at 21:31:15
for /f "tokens=2 delims=." %%a in ('dir /b *.txt') do if exist %%a.pdf move %%a.pdf d:\image\001

It was not clear to me on what condition you move the files to 001 or 002 or 003 directories.

--
Holla.


Report •

#2
January 21, 2009 at 06:53:23
Lets say there are 100 files 50 are pdf's and 50 are txt the first match goes into 001 the next 002........ so each pair goes into a directory until nothing is left.

Report •

#3
January 21, 2009 at 22:27:38
@echo off
setlocal EnableDelayedExpansion
for /f "tokens=2 delims=." %%a in ('dir /b *.txt') do (
  if exist %%a.pdf (
    set /a i+=1
    if /i !i! GTR 3 set i=1
    if not exist d:\image\00!i! md d:\image\00!i!
    move %%a.pdf d:\image\00!i!
  )
)

--
Holla.


Report •

Related Solutions

#4
January 22, 2009 at 08:34:59
it has to move 1 pdf and 1 text file with the same name

Report •

#5
January 22, 2009 at 11:24:04
I think i have somthing. is there a way to make it better so I dont have to have such a big bat file? Right now it goes down to D:\IMAGE\057. Also if there is a pdf and no corresponding txt file or the other way around, I will never know.

@echo off

for /F "tokens=1-2 delims=." %%a in ('dir /b *.txt') do SET FILE=%%a
for /F "tokens=2 delims=." %%B in ('dir /b *.txt') do SET NAME=%%B
MOVE %FILE%.%NAME%.IDX.TXT D:\IMAGE\001
IF EXIST %NAME%.PDF MOVE %NAME%.PDF D:\IMAGE\001

for /F "tokens=1-2 delims=." %%a in ('dir /b *.txt') do SET FILE=%%a
for /F "tokens=2 delims=." %%B in ('dir /b *.txt') do SET NAME=%%B
MOVE %FILE%.%NAME%.IDX.TXT D:\IMAGE\002
IF EXIST %NAME%.PDF MOVE %NAME%.PDF D:\IMAGE\002

for /F "tokens=1-2 delims=." %%a in ('dir /b *.txt') do SET FILE=%%a
for /F "tokens=2 delims=." %%B in ('dir /b *.txt') do SET NAME=%%B
MOVE %FILE%.%NAME%.IDX.TXT D:\IMAGE\003
IF EXIST %NAME%.PDF MOVE %NAME%.PDF D:\IMAGE\003


for /F "tokens=1-2 delims=." %%a in ('dir /b *.txt') do SET FILE=%%a
for /F "tokens=2 delims=." %%B in ('dir /b *.txt') do SET NAME=%%B
MOVE %FILE%.%NAME%.IDX.TXT D:\IMAGE\004
IF EXIST %NAME%.PDF MOVE %NAME%.PDF D:\IMAGE\004

for /F "tokens=1-2 delims=." %%a in ('dir /b *.txt') do SET FILE=%%a
for /F "tokens=2 delims=." %%B in ('dir /b *.txt') do SET NAME=%%B
MOVE %FILE%.%NAME%.IDX.TXT D:\IMAGE\005
IF EXIST %NAME%.PDF MOVE %NAME%.PDF D:\IMAGE\005

for /F "tokens=1-2 delims=." %%a in ('dir /b *.txt') do SET FILE=%%a
for /F "tokens=2 delims=." %%B in ('dir /b *.txt') do SET NAME=%%B
MOVE %FILE%.%NAME%.IDX.TXT D:\IMAGE\006
IF EXIST %NAME%.PDF MOVE %NAME%.PDF D:\IMAGE\006

for /F "tokens=1-2 delims=." %%a in ('dir /b *.txt') do SET FILE=%%a
for /F "tokens=2 delims=." %%B in ('dir /b *.txt') do SET NAME=%%B
MOVE %FILE%.%NAME%.IDX.TXT D:\IMAGE\007
IF EXIST %NAME%.PDF MOVE %NAME%.PDF D:\IMAGE\007


Report •

#6
January 23, 2009 at 01:06:15
Sorry,
I thought only 3 folders (001, 002 and 003).
My mistake.

The following would do what you want:

@echo off
setlocal EnableDelayedExpansion
for /F "tokens=1-2 delims=." %%a in ('dir /b *.txt') do (
  if exist %%b.pdf (
    set /a i+=1
    set imgdir=00!i!
    if /i !i! GTR 9 set imgdir=0!i!
    if /i !i! GTR 99 set imgdir=!i!
    if not exist d:\image\!imgdir! md d:\image\!imgdir!
    move %%b.pdf d:\image\!imgdir!
    move %%a.%%b.idx.txt d:\image\!imgdir!
  ) else (
  echo No Corresponding PDF exist for %%a.%%b
  )
)

for %%c in (*.pdf) do if not exist *.%%~nc.idx.txt echo No Corresponding text file exists for %%c
  

--
Holla.


Report •

#7
January 23, 2009 at 07:18:43
Holla, I think you got it except right now i have d:\image\001 - 056, when i ran what you gave me it skipped all my dir's and created 063, 064, 065......... We are 99.9% there!!

Thank you very much for all your help!!


Report •

#8
January 23, 2009 at 19:02:38
I am clueless on why it starts from 63.
What is the name of the batch file you are
running?

Please post the content of the batch file you
are using without any modifications.

--
Holla.


Report •

#9
January 26, 2009 at 07:35:49
It Works!! If i delete all the directorys under d:\image, then run what you gave me it creates all the directorys. It doesnt work if the dir's already exist. I can live with that.


Thank you very much for all your help!!

Moshef


Report •


Ask Question