Solved

Batch File - Check file exists

Posted on 2014-02-06
4
4,769 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
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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
A short article about a problem I had getting the GPS LocationListener working.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

919 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now