Solved

Batch File - Check file exists

Posted on 2014-02-06
4
4,727 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 51

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

746 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

12 Experts available now in Live!

Get 1:1 Help Now