0

I need a oneliner to wait for a file until it is created.

Is there a way to write that as a windows batch command? I cant have GOTO in it because I use pushd.

something like:

IF NOT EXIST \\blablabal\myfile.txt  sleep(30)
user1423277
  • 109
  • 2
  • 13
  • There would be in PowerShell... `while (-not (Test-Path myfile.txt)) { Sleep -Seconds 1 }` - how is pushd incompatible with goto? – TessellatingHeckler Nov 26 '15 at 09:10
  • I don't understand why you can't use GOTO. Maybe you should show us your **complete** batch file –  Nov 26 '15 at 09:29
  • If I use it in powershell is there a way to use pushd to connect to the drive as well then? – user1423277 Nov 26 '15 at 09:34
  • You want a one-liner, so I suppose you want to execute it in the command prompt (`cmd`); it that true? if so, you should adapt the tags accordingly (add [tag:cmd]?); `goto` will not work in `cmd`, so I think this cannot be done in `cmd` directly... – aschipfl Nov 26 '15 at 12:48

3 Answers3

3

The solution below is a one-liner that should be executed at the command-prompt, as requested (a Batch file is not a "one-liner"):

cmd /C for /L %i in () do if not exist \\blablabal\myfile.txt (timeout /T 30 ^>NUL) else exit

If you wish, you may insert this line in a Batch file doubling the percent sign.

Aacini
  • 65,180
  • 12
  • 72
  • 108
0

I'd suppose that you want to avoid goto statement and :label inside a parenthesized code block. Use call as follows:

(
     rem some code here
     call :waitForFile
     rem another code here
)
rem yet another code here

rem next `goto` skips `:waitForFile` subroutine; could be `goto :eof` as well
goto :nextcode
:waitForFile
  IF EXIST \\blablabal\myfile.txt goto :eof
  TIMEOUT /T 30 >NUL
goto :waitForFile

:nextcode

However, if you need a oneliner to wait for a file until it is created, written as a windows batch script: save next code snippet as waitForFile.bat

@ECHO OFF
SETLOCAL EnableExtensions
:waitForFile
  IF EXIST "%~1" ENDLOCAL & goto :eof
  TIMEOUT /T 30 >NUL
goto :waitForFile

Use it as follows:

  • from command line window: waitForFile "\\blablabal\myfile.txt"
  • from another batch script: call waitForFile "\\blablabal\myfile.txt"

Be sure that waitForFile.bat is present in current directory or somewhere in path environment variable.

Community
  • 1
  • 1
JosefZ
  • 28,460
  • 5
  • 44
  • 83
0

cmd /c "@echo off & for /l %z in () do (if EXIST c:\file.ext exit)"

Hammers the cpu though...

garbb
  • 679
  • 5
  • 9