How to check write a file text completed to execute another task using batch script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Noobknight
Posts: 5
Joined: 18 Nov 2018 20:31

How to check write a file text completed to execute another task using batch script

#1 Post by Noobknight » 18 Nov 2018 20:35

I have two files are env.txt and variable.txt. I'm using batch script to read all value in file env.txt and replace them in file variable.txt, after replaced all values, I want to execute a schedule task.
My issue: Read - Write file working fine, but I don't know how to check variable.txt file write completed to execute another task and I want handle this synchronize.
Detail:
File env.txt contains some value
Ex:
value1
value2

When I reading file env.txt line by line, I want replace each this value assign to file variable.txt and execute a task.
Here my code:

Code: Select all

@echo off
setlocal enableextensions enabledelayedexpansion
rem Defined some variables...
for /F "delims=" %%a in (%ENV_FILE_PATH%) do (
    set /A count+=1
    set "array[!count!]=%%a"
)
set newline=^& echo.
set new_file=
for /L %%i in (1,1,%count%) do (
    set replace_host=TESTED_HOST="!array[%%i]!"
    FOR /F "usebackq delims=" %%a in (%VARIABLE_FILE_PATH%) do (
        set "var=%%a"
        echo(!var!|findstr /r /c:"!search!" >nul && (
            set "var=!replace_host!"
        )
        echo(!var!|findstr /r /c:"!search_user_name!" >nul && (
            set "var=!default_user_admin!"
        )
        echo(!var!|findstr /r /c:"!search_pwd!" >nul && (
            set "var=!default_pwd_admin!"
        )
        set new_file=!new_file!%newline%!var!
    )
    echo(!new_file!) > !FILE_VARIABLE_OUT!
    rem ( (call ) >>!FILE_VARIABLE_OUT! ) 2>nul && (
    rem     Execute task when check file write completed
    rem     echo The file write completed
    rem ) || (
    rem     echo The file is still being created
    rem )
)
endlocal
pause
Thanks all !

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to check write a file text completed to execute another task using batch script

#2 Post by Ed Dyreen » 21 Nov 2018 07:56

This is a bit odd to me

Code: Select all

    echo(!new_file!) > !FILE_VARIABLE_OUT!
    rem ( (call ) >>!FILE_VARIABLE_OUT! ) 2>nul && (
    rem     Execute task when check file write completed
    rem     echo The file write completed
    rem ) || (
    rem     echo The file is still being created
    rem )
Do you expect CMD to continue with the next command before it completed the previous

Code: Select all

echo(!new_file!) > !FILE_VARIABLE_OUT!
command ? Because that doesn't happen, a write either fails or succeeds. During writes the script waits for the command to complete.

Noobknight
Posts: 5
Joined: 18 Nov 2018 20:31

Re: How to check write a file text completed to execute another task using batch script

#3 Post by Noobknight » 21 Nov 2018 08:21

"command ? Because that doesn't happen, a write either fails or succeeds. During writes the script waits for the command to complete." - Yes, I want after replace value in to file variable.txt and write it complete then execute a cron job using schtask

Can say is I want to execute a cron job via values read from file variable.txt

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to check write a file text completed to execute another task using batch script

#4 Post by Ed Dyreen » 21 Nov 2018 08:27

Noobknight wrote:
21 Nov 2018 08:21
"command ? Because that doesn't happen, a write either fails or succeeds. During writes the script waits for the command to complete." - Yes, I want after replace value in to file variable.txt and write it complete then execute a cron job using schtask

Can say is I want to execute a cron job via values read from file variable.txt
Since the script waits for the write to complete, you cannot detect write completion from the process doing the write, only a secondary process can detect locked files.

for example process one

Code: Select all

> file (
	echo.contents
	pause
) &&echo.succes ||echo.failes
The pause locks the file for the test.

process two

Code: Select all

> file (
	echo.contents
) &&echo.succes ||echo.failes
This now fails.

Multi threading in dos is normally achieved by using shared memory or shared files between processes.
and you would also need to make it impossible for a process to write or read while another process is reading or writing.
The above snippets are an example of a technique that can be used to create such a filemutex.

Noobknight
Posts: 5
Joined: 18 Nov 2018 20:31

Re: How to check write a file text completed to execute another task using batch script

#5 Post by Noobknight » 22 Nov 2018 05:38

Ed Dyreen wrote:
21 Nov 2018 08:27
Noobknight wrote:
21 Nov 2018 08:21
"command ? Because that doesn't happen, a write either fails or succeeds. During writes the script waits for the command to complete." - Yes, I want after replace value in to file variable.txt and write it complete then execute a cron job using schtask

Can say is I want to execute a cron job via values read from file variable.txt
Since the script waits for the write to complete, you cannot detect write completion from the process doing the write, only a secondary process can detect locked files.

for example process one

Code: Select all

> file (
	echo.contents
	pause
) &&echo.succes ||echo.failes
The pause locks the file for the test.

process two

Code: Select all

> file (
	echo.contents
) &&echo.succes ||echo.failes
This now fails.

Multi threading in dos is normally achieved by using shared memory or shared files between processes.
and you would also need to make it impossible for a process to write or read while another process is reading or writing.
The above snippets are an example of a technique that can be used to create such a filemutex.
I tried using your script to apply my script, but I don't know how to write content !new_file! into file !FILE_VARIABLE_OUT!. Content of file always empty!

Here my code:

Code: Select all

> !FILE_VARIABLE_OUT! (
	echo.!new_file!
	pause
) &&echo.succes ||echo.failes

Post Reply