Solved

Batch File - Check file exists

Posted on 2014-02-06
4
4,942 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 54

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
SSRS - Powershell 7 80
Why is enum singleton a better approach than static factory 3 65
WordPress: Debugging from my Windows 10 Desktop 6 93
Python to .bat or Powershell 2 73
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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 …

732 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