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
Solved

Batch File - Check file exists

Posted on 2014-02-06
4
4,856 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 53

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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 …

861 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