Solved

Batch File - Check file exists

Posted on 2014-02-06
4
5,013 Views
Last Modified: 2014-02-13
I don't know if this can be done with batch scripting, but I need to check in a folder to see if all the text files in that folder already exists in another or not.  Or if I can read the network folder.

I have a folder called "JOBS" that has 2 sub folders "Archive" and "Error".  The bat script will copy all files with extension "txt" into a network folder "\\bb\nlc\tr\JOBS".  Once it's done copying, it'll check and see if the same file from "JOBS" exists in the network folder "JOBS".  If it does not exists or can't read the network folder, move all the files into a folder called "Error".  If it exists, then move it to a folder called "Archive".

Is this even doable with batch jobs?  I know the moving and copying can be done.  Just not sure about the checking if a file exists or can read/see the network folder.
0
Comment
Question by:holemania
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39840364
You would need to do it on a one by one basis, but you can do it:

for %%f in ("MyDir\*.txt") do (

             if exist %%f (
                 rem file exists -> do something
             ) else (
                 rem file doesn't exist -> do something else.
             )

    )



type "help if" in a command prompt for more info.
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 100 total points
ID: 39840569
Something along these lines sounds like what you are after.... can always add in some logging or error checking etc. as needed.

Steve

@Echo off

Set SourceDir=C:\jobs
Set DestDir=\\bb\nlc\tr\JOBS

CD /d "%Sourcedir%"

xcopy /y /d *.txt "%DestDir%"

For /f "tokens=*" %%a in ('dir /b /a-d %Sourcedir%\*.txt') do (
  if exist "%DestDir%\%%~a" (
    MOVE "%Sourcedir%\%%~a" "%Sourcedir%\Archive"
  ) ELSE (
    MOVE "%Sourcedir%\%%~a" "%Sourcedir%\Error"
  )
)

Open in new window

0
 
LVL 55

Accepted Solution

by:
Bill Prew earned 400 total points
ID: 39840738
Here is a slightly different approach. During the COPY command we check for success or fail and move the file being copied to the appropriate folder.  Seems a little cleaner...

@echo off
setlocal

set BaseDir=c:\jobs
set GoodDir=archive
set BadDir=error
set DestDir=\\bb\nlc\tr\jobs

for %%A in ("%BaseDir%\*.txt") do (
  copy "%%~A" "%DestDir%" && (
    move "%%~A" "%BaseDir%\%GoodDir%"
  ) || (
    move "%%~A" "%BaseDir%\%BadDir%"
  )
)

Open in new window

~bp
0
 

Author Closing Comment

by:holemania
ID: 39856975
Awesome.  Thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This is about my first experience with programming Arduino.
In this post we will learn different types of Android Layout and some basics of an Android App.
Simple Linear Regression
Introduction to Processes

717 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