DanCode
DanCode

Reputation: 673

Batch Script: how to move and replace existing file

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?

Upvotes: 1

Views: 6459

Answers (1)

Ben Personick
Ben Personick

Reputation: 3264

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.

Upvotes: 1

Related Questions