1

My job is to find the string "WebDriver Sampler" from the file sss_output.csv, store the results in a temporary file tmp.csv.

Then move the file tmp.csv to replace the existing file sss_output.csv.

Here is my code:

call findstr /C:"WebDriver Sampler" sss_output.csv > tmp.csv
call move /Y tmp.csv sss_output.csv

The problem I am encountering is if the file sss_output.csv exist, then the data from the file tmp.csv will append to the sss_output.csv file.

I want the data from tmp.csv to replace the data in sss_output.csv.

How can I do that?

1
  • Well, move does not append to existing files, it replaces files; are you sure you provided a minimal reproducible example? And why are you using call? there is no point in doing so here... Commented Jun 25, 2019 at 19:50

1 Answer 1

1

There is no need to CALL Findstr, you only need to call asynchronous processes you want to wait for completion before continuing or you're calling a label in a cmd script, its not going to hurt anything in all probability, but might as well just remove that bit as it isn't necessary.

So that would mean:

FindStr /I /C:"WebDriver Sampler" sss_output.csv > tmp.csv
MOVE /Y tmp.csv sss_output.csv

While Move "should" over-write the file I also experienced this behavior in the past when doing the needful within a loop, as there seemed to be a process still holding the file open and causing me to append.

So, if there is nothing holding the original file open, Just delete it.

IE:

( FindStr /C:"WebDriver Sampler" "sss_output.csv" > "tmp.csv" )
IF NOT EXIST "tmp.csv" ( COLOR CF & ECHO.ERROR WE DID NOT CREATE THE TEMP FILE! The String was not Matched!) ELSE ( ( DEL /F/Q sss_output.csv && MOVE /Y "tmp.csv" "sss_output.csv"  ) || ( COLOR E0 & ECHO.SOMETHING CAUSED US NOT TO BE ABLE TO DELETE THE FILE OR THE File COuld Not be copied! ) )

If there is a process holding it open or the file "always needs to exist" for some other reason, then you can just Replace the content with the content from tmp.csv by Typeing it into the original file instead.

IE:

( FindStr /I /C:"WebDriver Sampler" "sss_output.csv" > "tmp.csv" )
IF NOT EXIST "tmp.csv" ( COLOR CF & ECHO(ERROR WE DID NOT CREATE THE TEMP FILE! The String was not Matched!) ELSE ( ( Type "tmp.csv" > "sss_output.csv" && DEL /F/Q "tmp.csv"  ) || ( COLOR B0 & ECHO(SOMETHING CAUSED US NOT TO BE ABLE TO WRITE TO THE FILE ) )

The above will Echo the single matching line into the temp file, and then, if that was successful (meaning a match had been found in the first place) it will replace the contents of the Original file with those of the temp file. If that was successful it will then delete the temp file.

Also, this method will do the same without creating a temp file:

FOR /F "Tokens=*" %%A IN ('FindStr /I /C:"WebDriver Sampler" "sss_output.csv"') DO @(
   SET "_Line=%%A" & DIR 1>&0 2>NUL 
)
IF DEFINED _Line (
   ECHO(%_LINE%>"sss_output.csv"
   COLOR A0
   ECHO(We Found this Line and tried to write it to the file:
   ECHO(%_LINE%
) ELSE (
   COLOR CF
   ECHO(ERROR: The String was not Matched!
)

I am using the DIR 1>&0 to break out of the loop early. ( I used to use echo.hello>&3 but I find DIR 1>&0works more consistently)

I have amended these to be on separate lines so that we are not relying on &&s which may be holding the file open from the previous step.

I am checking to see if the temp file is created (which if not then we did not match the line, or we did not have permission to create the temp file) I am also checking if we matched the line in the Temp file -less method (as I always was) but in all cases I am echoing out some dialog and changing the font color depending on which step it is failing on.

See what the result sof running eahcof these are please.

Sign up to request clarification or add additional context in comments.

2 Comments

both of these suggestions did not work. File still gets append.
Both meaning, Deleting the file and then moving the temp? and Replacing the content with the Echo in all methods supplied? Might be that && is holding the file open even still. So I'll amend to have that on a separate line. Try that. If not, then some other process ( other than the cmd session you're working in) is holding your file open and not allowing the existing content to be replaced.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.