Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Check if file exists with batch script  (Read 147127 times)

0 Members and 1 Guest are viewing this topic.

jc

  • Guest
Check if file exists with batch script
« on: June 12, 2007, 06:32:46 AM »
I have a bat file that I want to kick off but then wait until a certain file gets downloaded to execute the remainder of the script.  How can I check to see if a file exists on a recurring basis (i.e. loop) in the script without causing excessive resource usage on the machine (i.e. how can I leave some space between the checks).

Carbon Dudeoxide

  • Global Moderator

  • Mastermind
  • Thanked: 169
    • Yes
    • Yes
    • Yes
  • Certifications: List
  • Experience: Guru
  • OS: Mac OS
Re: Check if file exists with batch script
« Reply #1 on: June 12, 2007, 06:37:43 AM »
Assuming you have Windows XP, this might work:

Code: [Select]
@echo off
:1
if exist "%userprofile%\desktop\file.exe" (
echo The file is ready
pause
exit
) else (
goto :1
)

Is this something like what you need?

jc

  • Guest
Re: Check if file exists with batch script
« Reply #2 on: June 12, 2007, 06:50:54 AM »
that looks like what I need.  how long does the 'pause' cause the program to wait before moving to the next command?

Carbon Dudeoxide

  • Global Moderator

  • Mastermind
  • Thanked: 169
    • Yes
    • Yes
    • Yes
  • Certifications: List
  • Experience: Guru
  • OS: Mac OS
Re: Check if file exists with batch script
« Reply #3 on: June 12, 2007, 07:15:54 AM »
I see your new to this...

Basically if the file exists, it will say on the screen "The file is ready" and "Press any key to continue. . ."
This means it stops (forever) until you press any key. When you press any key, the program will close.

jc

  • Guest
Re: Check if file exists with batch script
« Reply #4 on: June 12, 2007, 07:32:53 AM »
okay then this is not what I need.  Here's my flow.

1)  I have an ftp script that runs and downloads files from a unix server to a windows server.
2)  I then have processing that occurs via two scripts that are scheduled via a scheduling package my company uses.
3) The final step is an access database that I can't seem to get to work correctly when I try to run it scheduled (it just hangs, can't figure out why).  however when I run it out of task scheduler it works fine.

so what I want to do is

1)  create a file as the final step in one of the two scripts in #2
2)  schedule the access database to run inside of a bat file which will kick off from task scheduler.  inside that bat file would be a file check routine that does the following:

a)  check to see if the file exists
b)  wait some period of time
c)  check to see if the file exists

and continue looping until the file does exist, then move on to the next step in the code.  is this doable?

Carbon Dudeoxide

  • Global Moderator

  • Mastermind
  • Thanked: 169
    • Yes
    • Yes
    • Yes
  • Certifications: List
  • Experience: Guru
  • OS: Mac OS
Re: Check if file exists with batch script
« Reply #5 on: June 12, 2007, 07:38:32 AM »
Is it doable.....
What operating system are you using and how long do you want the batch file to wait for?

Also, if the batch file sees the file exists, you can start it with the batch file.

I actually have to go so someone will be along shortly to answer your question  ;)

jc

  • Guest
Re: Check if file exists with batch script
« Reply #6 on: June 12, 2007, 07:40:41 AM »
windows server 2003, and it could be anywhere from being there when the script starts to several hours.  I think if the script could check every five minutes until it finds that the file exists that would be perfect.

ghostdog74



    Specialist

    Thanked: 27
    Re: Check if file exists with batch script
    « Reply #7 on: June 12, 2007, 08:27:25 AM »
    Code: [Select]
    @echo off
    :loop
    if not exist "c:\somefile" Goto Loop
    echo "finish"

    GuruGary



      Adviser
      Re: Check if file exists with batch script
      « Reply #8 on: June 12, 2007, 09:09:51 AM »
      Ghostdog's code should work, but checks continuously and may be processor intensive for a batch file.  The section of code to check for the file every 5 minutes (using PING as your timer) could be something like this:

      Code: [Select]
      @echo off
      rem The first part of your code goes here
      :CheckForFile
      if exist "C:\Path\File.ext" goto FileExists
      ping -n 300 localhost >NUL
      goto :CheckForFile
      :FileExists
      rem The rest of your code goes here